Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make a gameObject rotate/face the same direction as raycast hit gameObject

Discussion in 'Editor & General Support' started by Acer85, Jun 20, 2021.

  1. Acer85

    Acer85

    Joined:
    Mar 10, 2019
    Posts:
    28
    I have a gameObject that I position at same position after a raycast hits another gameObject but I have a problem when I try to roatate it at the same direction.
    The gameObject that I position to the hit gameObject also does not get a centerd pivit point.
    Is there a simpel way to solve this like face direction or something.
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,494
    You could try transform.up = hit.normal;
    Edit: or transform.forward = hit.normal;
    Edit 2: and can use -hit.normal for opposite direction
     
    Acer85 likes this.
  3. Acer85

    Acer85

    Joined:
    Mar 10, 2019
    Posts:
    28
    Thank you for the answer #polemical but I want the object to always face the same way.

    I think Im on to something maybe. I made another cube on each object that I can hit and then use LookAt(...positon, vector.down). It works fine but the right side is not facing the lookAt transform positon.
    I have tried vector.up, left, right but with no success.
    Any ides how to solve this?
     
    Last edited: Jun 22, 2021
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,494
    I only know a few things to try, I'm no expert on this - but what else comes to mind is there's also
    transform.LookAt
    , and
    transform.rotation = Quaternion.LookRotation(position1 - position2);
    also you could try something with .rotation = Quaternion.Euler(.. with whatever.. maybe that stuff helps with ideas for experimenting until something works or until someone else has more to suggest.
     
    Acer85 likes this.
  5. Acer85

    Acer85

    Joined:
    Mar 10, 2019
    Posts:
    28
    Well I found one solution and it was quite simple.

    objectToRotate.transfom.forward = new Vector3(ohterObject.transfom.forward.x, ...y, ...z);

    and after that in my case I did a rotation

    objectToRotate.transform.Rotate(0f, 90f, 0f, Space.Self);
     
    adamgolden likes this.