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

transform.LookAt Help

Discussion in 'Scripting' started by raeldor, Sep 5, 2014.

  1. raeldor

    raeldor

    Joined:
    Jul 8, 2013
    Posts:
    55
    Hi All,

    Looking for a little help with the transform.LookAt function. I have an object that is rotated so that it's 'up' is not world up. I want to have that object look at a point in world space and to be able to manipulate that point so that 'y' is 'up'. However, when I move that point upwards along the 'y axis and then try and look at that point using transform.LookAt(targetPos, new Vector3(0,1,0)) my object does not rotate upwards in the world 'Y' direction.

    I'm sorry if I'm not explaining this very well, but I don't entirely understand what's going wrong. Would I need to somehow translate the movement of my point into something that is compatible with the objects 'up' and then use the objects 'up' in the LookAt function?

    Thanks
    Ray
     
  2. Nielssss

    Nielssss

    Joined:
    Nov 29, 2012
    Posts:
    6
    you might want to try:

    Code (CSharp):
    1. transform.LookAt(targetPos, new Vector3(0,1,0),Space.World)
    EDIT: I am not entirely sure if this is possible for lookat though. I will look into it more
     
  3. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    You can also make the rotated object a child of another gameobject so you can hide the internal rotation.
     
  4. raeldor

    raeldor

    Joined:
    Jul 8, 2013
    Posts:
    55
    Thanks for the replys. Is it quite common to do this? It seems that LOOKAT would only work if the rotation of the object is such that the local 'forward' matches the object's forward, but it seems that's not always the case. Am I wrong, or am I making this seem more complicated than it is?