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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Quaternion.LookRotation() regarding parent

Discussion in 'Scripting' started by xarapuchatok, Jan 9, 2020.

  1. xarapuchatok

    xarapuchatok

    Joined:
    Dec 27, 2019
    Posts:
    11
    My 3D object consists of 2 parts:
    base
    turret
    when I find enemy and try to rotate my turret to it - work nice!

    Code (CSharp):
    1. turretlookAtTarget = target.transform.position - transform.position;
    2.  
    3. turretRot = Quaternion.LookRotation(turretlookAtTarget);
    4. transform.rotation = Quaternion.Slerp(transform.rotation, turretRot, RotateSpeed * Time.deltaTime);
    5.  
    But when my 3d object move up or down on hill turret look at target in global axis (or I don't know), not as a part of base and it looks like not in real life(( How create lookAtRotation regarding my base? Please Help!
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You can either set the y value of both the vectors to 0 or the same value before calculating the rotation so it doesn't rotate on the y-axis or you can restrict the rotation on the y-axis. It depends on what you are trying to achieve.
     
  3. xarapuchatok

    xarapuchatok

    Joined:
    Dec 27, 2019
    Posts:
    11
    This works , thanks!
    Code (CSharp):
    1. transform.localEulerAngles = new Vector3 (0, transform.localEulerAngles,0);