Skip to content

Instantly share code, notes, and snippets.

@Kurukshetran
Forked from jakevsrobots/PickUpObject.cs
Created November 4, 2015 02:54
Show Gist options
  • Save Kurukshetran/1a002282ed3793693153 to your computer and use it in GitHub Desktop.
Save Kurukshetran/1a002282ed3793693153 to your computer and use it in GitHub Desktop.
Script for picking up objects in Unity.
using UnityEngine;
using System.Collections;
public class PickUpObject : MonoBehaviour {
public Transform player;
public float throwForce = 10;
bool hasPlayer = false;
bool beingCarried = false;
void OnTriggerEnter(Collider other)
{
hasPlayer = true;
}
void OnTriggerExit(Collider other)
{
hasPlayer = false;
}
void Update()
{
if(beingCarried)
{
if(Input.GetMouseButtonDown(0))
{
rigidbody.isKinematic = false;
transform.parent = null;
beingCarried = false;
rigidbody.AddForce(player.forward * throwForce);
}
}
else
{
if(Input.GetMouseButtonDown(0) && hasPlayer)
{
rigidbody.isKinematic = true;
transform.parent = player;
beingCarried = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment