-
-
Save Kurukshetran/1a002282ed3793693153 to your computer and use it in GitHub Desktop.
Script for picking up objects in Unity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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