Search Unity

problem when turning the object

Discussion in 'Scripting' started by Trild123787898, Feb 19, 2020.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    I have object1 that I am making a turn, and there is another object2 along the way, I need that when object1 collides with object2, object1 can make a turn only in the opposite direction, so that it cannot go through!

    here is the object rotation script

    Code (CSharp):
    1. if (click_detail3 == true)
    2.         {
    3.             detail3.Rotate(new Vector3(0, 0.3f, 0));
    4.            
    5.         }
    6.         if (click1_detail3 == true)
    7.         {
    8.             detail3.Rotate(new Vector3(0, -0.3f, 0));
    9.            
    10.         }
     
  2. KaiMiraGames

    KaiMiraGames

    Joined:
    Jan 2, 2020
    Posts:
    17
    It sounds like you're trying to replicate behavior of Rigidbodies - you might solve this by just adding a rigidbody to both of your objects (assuming they already have colliders on them as well).

    If you need to do it manually, then setting the rotation might not be enough - you might need to change the velocity as well. If you have an object moving to the right and it collides with something, it will need to change rotation (face the other way) and velocity (move the other way).
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    rigidbody is the way out for me, for this there is a good reason