Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Vector3 Reflect, Setting new rotation

Discussion in 'Scripting' started by Grafer, Apr 22, 2017.

  1. Grafer

    Grafer

    Joined:
    May 30, 2015
    Posts:
    18
    Hi there.
    I'm trying to make my bullets richochet after hitting walls. I am using Vector3 Reflect. And it works as long as I work with raycasts. However I have no idea how to set new object's rotation in order for it to move in new direction

    Code (CSharp):
    1.     private void OnCollisionEnter(Collision collision)
    2.     {
    3.         ContactPoint contacts = collision.contacts[0];
    4.         Vector3 reflectDir2 = Vector3.Reflect(transform.forward, contacts.normal);
    5.  
    6.         //transform.rotation.SetFromToRotation(transform.forward, Mathf.Deg2Rad * reflectDir2);
    7.         //transform.rotation = Quaternion.FromToRotation(Quaternion.ToEulerAngles(transform.rotation), reflectDir2);
    8.     }
    I've tried many possibilities, I've read dozens of topics but I couldn't make anything work. What may be the cause, How should I approach this?

    This is how it looks at the moment
    https://i.gyazo.com/df77d58683e61e73c771d217b8d06f15.mp4

    .
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    906
    Code (csharp):
    1. transform.rotation = Quaternion.LookRotation(reflectDir2);
    Should do the trick I think. It sets the rotation to point in the direction of the vector.