- Home /
Stopping an object's momentum,Stopping a 3d object's momentum
I am working on a VR project in Unity that works with the Oculus Quest. I have gotten past all of the basics of the character and am working on the objects. I am trying to make it so that they are able to respawn after falling off the edge of the floating island that they are on. I found a script that allows this to happen, but the objects that I want to be bouncy are getting stuck in an infinite loop of bouncing higher and higher and then respawning if they fall off the edge. I want to know if there is a way to stop the object's momentum when it respawns, or if there is another way to fix this.
Current script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectRespawn : MonoBehaviour
{
public float threshold = -50f;
void FixedUpdate () {
if (transform.position.y < threshold)
transform.position = new Vector3(0, 1, 1);
}
}
Answer by chor64 · Jun 03, 2020 at 04:50 AM
Just get the rigidbody and set the velocity to zero, like so:
Rigidbody rb = GetComponent<Rigidbody>();
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
If you're using this script on something that doesn't have a rigidbody, before setting the values, check if rb is not null.