- Home /
Object change direction after collide with controller
I have got a gameobject that is attached with a box collider and physic material. The hand controller model is also attached to a box collider and physic material. When the gameobject collides with the hand controller, the 'CollideWithController' logs on the console. However, the gameobject does not change direction.
Answer by theterrificjd · Dec 13, 2017 at 10:56 AM
First thing, you should have your Rigidbody stored in a Rigidbody variable when your Class starts up. This will save you from running the GetComponent function every collision.
Other than that, this code snippet looks like it would work. I would add more info into your Debug.Log, something like:
Debug.Log("Direction:" + force.ToString());
Try calling it before and after your AddForce line. That way, if it's called twice, you know it tried to add the force. If it only calls once, you know that's where its cutting out (could be a null reference to the rigidbody).
I have tried your method. The Debug.Log("Direction:" + force.ToString()); is being called twice, however, the gameobject still does not change direction after the collision.
Answer by RisingDead_xTR · Dec 14, 2017 at 04:08 AM
I think the direction of force is facing the HandController. To check that, try to do something like :-
var force = new Vector3 (0,0,-1);
With that much of info that's all i can do. Hope it helps :)
Hi, how do i check the force using this? Sorry i am relatively new to unity so thanks so much for your help!
Hey, sorry for the late reply. I tried to do what you are doing and ended up with same result. But when i increased magnitude from 1,000 to 10,000... it worked! I also found out that it was becoz of my initial speed of the gameObject was very high and .AddForce(...) could not make it turn around but nullified the force. Also this problem didn't happen when i was using Collisions rather than Triggers. It may work for you too. Tell me if it doesn't.
Remember you can always do something like transform.GetComponent().velocity = Vector3.zero;
and then add the force.
Yes. That also explain why the problem doesn't occur when using Collisions as during a collision the velocity gets reduced.
@RisingDead_xTR Hi! Sorry for the late reply. I tried to increase the force magnitude to 100000000, but it still doesnt work even though it logs on the console. I tried to use to OnCollisionEnter but it doesnt even log on the console?!
Hi, i attached this script to my gameobject, not the hand controllers, does it matter?