Last active
July 10, 2021 00:13
-
-
Save jakevsrobots/7416643 to your computer and use it in GitHub Desktop.
Script for picking up objects in Unity.
This file contains hidden or 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
set ur collider to is trigger or something idk