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

Vector maths help - Rotate object toward other on 1 axis

Discussion in 'Scripting' started by SunnySunshine, Jun 11, 2014.

  1. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Take these two objects:



    I want to rotate the plane so that its Y direction is facing the cube (as closely as possible), but the plane may only rotate around its local X axis. How to calculate this?

    Thank you.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    To look at the object use:

    public Transform target;

    void Update()

    {
    transform.LookAt(target);
    }

    I would sudjest you to rotate the object using Space.World.

    transform.Rotate (Vector3.up * Time.deltaTime, Space.World);
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    That doesn't make any sense, it's impossible to do what you want if this is your condition
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    try, plane.up = box.position - plane.position.
     
  6. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Can you elaborate? I tried fetching the angle from the transform positions, and using it in rotate, but the plane would just keep spinning.


    This will affect more than X axis.

    It doesn't need to face it 100%, just as closely as possible. They are using this technique in UE4 for light-beam effect. The rays are just a billboard basically with fixed position, but will rotate around one of its axis to face the camera.

    https://docs.unrealengine.com/latest/INT/Resources/Showcases/BlueprintExamples/FogSheet/index.html
     
    Last edited: Jun 11, 2014
  7. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Sort of works but affects Z axis rotation too. I only want X axis to be used.
     
  8. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    transform.rotate(new Vector3(Vector3.Angle(plane.up,(box.position-plane.position)),0,0);
     
  9. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Not sure if I'm doing something wrong but this is the result:

    http://gfycat.com/LeftScaredIndianjackal

    Code (CSharp):
    1.         transform.Rotate(new Vector3(Vector3.Angle(transform.up, (target.position - transform.position)), 0, 0));
    2.  
    Behavior added to Plane, target is cube transform.
     
  10. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Vector3 current = transform.localEulerAngles;
    transform.localEulerAngles = new Vector3(Vector3.Angle(transform.up, (target.position - transform.position),current.y,current.z);
     
  11. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Doesn't seem to do the trick:

    http://gfycat.com/BaggyUnfoldedIchidna

    Code (CSharp):
    1.         Vector3 current = transform.localEulerAngles;
    2.         transform.localEulerAngles = new Vector3(Vector3.Angle(transform.up, (target.position - transform.position)), current.y, current.z);
     
  12. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    Some thing like this?

    Lookat ( this.x, target.y, target.z)
     
  13. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    You're doing it in the update function, do you need to keep it updated?
     
  14. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    1) calculate the angle between the object and where the object would be in the z plane of the current object.
    2) rotate that object on the x axis that angle in self space....

    Code (csharp):
    1.  
    2.     Vector3 pos = transform.InverseTransformPoint (obj.position);
    3.      pos.x = 0;
    4.      float angle = Vector3.Angle (Vector3.up, pos);
    5.      angle = pos.z < 0 ? -angle : angle;
    6.      transform.Rotate (Vector3.right, angle, Space.Self);
    7.  

    This way, you ignore the difference in position from the object to the transform laterally. This allows you to rotate only in one axis and only in the space of the object.
     
    Last edited: Jun 11, 2014
    SunnySunshine likes this.
  15. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Yes, it needs to be kept updated.

    Sort of works, but behaves strangely when the other object is moving:

    http://gfycat.com/ComplicatedUnsteadyHorsemouse
     
  16. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Sorry, its pos.z, not pos.y.

    I corrected it in my post.
     
    SunnySunshine likes this.
  17. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Works splendidly. Thanks!