- Home /
How to rotate an object like item inspection when entering a trigger?
I want to rotate an object like item inspection when player enters a trigger. I've wrote the C# code for it, but I can't get the object to rotate while the player enters a trigger. I've tested the code without the trigger and I'm able to rotate the object in its place, but when there is a trigger and player enters it, I can't rotate the object. I want to rotate the object with W, S, A, D keys. What am I missing or doing wrong?
public class Trigger : MonoBehaviour {
public Rigidbody rb;
public float amount;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {}
private void OnTriggerEnter(Collider other)
{
Debug.Log("Entered");
float h = Input.GetAxis("Horizontal") * amount * Time.deltaTime;
float v = Input.GetAxis("Vertical") * amount * Time.deltaTime;
rb.AddTorque(transform.up * h);
rb.AddTorque(transform.right * v);
}
}
Answer by unity_J016ZU_VZQzYAg · Feb 20, 2019 at 04:42 PM
You're only reading the input and changing the rb that one time you Enter the Trigger. That's a one time call. You'd need to use OnTriggerStay
instead of OnTriggerEnter
.
What object is the script added to ? The object that is the trigger, needs to have that script added.
I have an object that has 2 box colliders that one of them works as a trigger and the script is added to the object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612192113im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Multiple Cars not working 1 Answer
How do i determin if an objects rotation is between 2 values on each axis? 0 Answers
How do you trigger a specific collider using OnTriggerEnter? 2 Answers
how do i make first person character rotate left and right along with camera? 0 Answers