Hi, I am pretty new to Unity and I generally us C#, but I managed to get the basics of this script working using JavaScript so I’ve stuck with it.
Basically, I’ve coded a very basic pick up object script that does just that. When the player collides with the box collider trigger, it allows the player to tap the ‘e’ key and then the object becomes a child of the camera. This works just fine but gravity is still applied. I tried simply disabling it when it’s picked up but then it floats away. I then tried making it kinematic but now it pushes the player backwards…so I’m well and truly stuck.
Any solutions to this?
var theObject : GameObject; //the object to pick up
var player : UnityEngine.Camera;
var canPickUp : boolean = false; //turns true when the player is close enough to the object
function Update ()
{
if(Input.GetKeyDown("e") && canPickUp)
{
theObject.transform.parent = player.transform;
theObject.GetComponent.<Rigidbody>().isKinematic = true;
}
}
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Player")
{
canPickUp = true;
}
}
function OnTriggerExit (info : Collider)
{
if(info.tag == "Player")
{
canPickUp = false;
}
}
Any help would be greatly appreciated, thanks!
Maybe try to set velocity as zero just after disabling the gravity? So it would look like: theObject.GetComponent.<Rigidbody>().useGravity = false; theObject.GetComponent.<Rigidbody>().velocity = Vector3(0,0,0);
– damiano201Just destroy the rigid body and re apply it when you want to drop it
– AR_RizvispawnValues = spawnPoints[randomPoint].position; Vector3 randomSpawnPoint = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z)); Instantiate (enemies[randEnemy], randomSpawnPoint, gameObject.transform.rotation);
– Priyanka-Rajwanshi