Search Unity

Is there a way to do something like a Local LookAt, a look to local place?

Discussion in 'Scripting' started by fermas, Sep 21, 2015.

  1. fermas

    fermas

    Joined:
    Sep 21, 2015
    Posts:
    15
    I am having a nightmare trying to make a child object rotate towards a given point of its parent object, similar to what is possible at the world level when using LookAt.

    The problem is that most functions related to rotating in Unity C# do not work for local level. In the description of function transform.Rotate one can have the impression that it allows that, trough passing the Space.Self parameter. However, to use that function one has to know the 3 angles between the 2 points of interest. And there is no function that allows such calculation.

    Anyone could please help implementing a local LookAt?
     
  2. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    And if you make an instance of an external object (helper) that looks at the target [parent] and then the child clones the rotation. The Awake starting rotation in the child probably need to be reset to the same as the parent?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    Why not just look at the point in world space?

    You could also transform the point from local to world space and look at it there, using Transform.TransformPoint, if the thing you're looking at is a local-space offset from the parent rather than a transform that already has a world position.
     
    LeftyRighty likes this.
  4. fermas

    fermas

    Joined:
    Sep 21, 2015
    Posts:
    15
    I cannot go for the solution of AlanMattano, first for efficiency issues: I will be rotating a lot of different objects at the same time in the scene and that solution would double the number of objects. Anyway, in the end it boils down to the same pattern of solution suggested by Baste.

    However, oddly enough, both suggestions do not work properly. Whenever I rotate the object in world space, it gets distorted, skewed like hell.