Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Limiting rotation to Y

Discussion in 'Scripting' started by nickavv, Jul 24, 2007.

  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Code (csharp):
    1. var target : Transform;
    2. var last : Transform;
    3. var accel = 2;
    4. static var lapsdone = 0;
    5. static var percent = 0.0;
    6. static var place = 0.0;
    7.  
    8. function FixedUpdate () {
    9.     if (Physics.Raycast (transform.position, Vector3.down, 1.5)) {
    10.         rigidbody.AddForce (Vector3.up * 7);
    11.     }
    12.     if (target.collider) {
    13.         var targetPoint = target.position;
    14.         var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
    15.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
    16.         var look = targetPoint - transform.position;
    17.         look.y = 0;
    18.         targetRotation = Quaternion.LookRotation (look, Vector3.up);  
    19.  
    20.     }
    21.     rigidbody.AddRelativeForce(Vector3.forward * accel);
    22.     var d1 = Vector3.Distance(transform.position, last.position);
    23.     var d2 = Vector3.Distance(transform.position, target.position);
    24.     var lerpVal = (d1/(d1+d2));
    25.     percent = Mathf.Lerp(last.GetComponent(Waypoints).progress, target.GetComponent(Waypoints).progress, lerpVal);
    26. }
    This is the code I use for RaceCraft's AI. The only problem is that the enemies now rotate on all axis to see the waypoints, but some of the waypoints centers are high in the air, causing the enemies to literally fly through the air to reach them. How would I go about limiting them to one axis?
     
  2. Harry1960

    Harry1960

    Joined:
    May 15, 2007
    Posts:
    136
    If you want to keep the height at a fixed point you can save your transforms y-coordinate before calculating the new position and restore it's value after the transformations.
     
  3. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    No, the tracks often vary in hight level. I also tried Transform.LookAt, which the documentation says is limited to y unless you add WorldUp, but it was lying! :eek:
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    You misread: worldUp is a hint vector. That is, it will be on the local YZ plane after the function is called.

    What you can try is use is:

    Code (csharp):
    1. targetRotation = transform.rotation.SetLookRotation(ZeroY(target.position));
    ...where ZeroY is:

    Code (csharp):
    1. function ZeroY(vector : Vector3) : Vector3 {
    2.     return new Vector3(vector.x, transform.position.y, vector.z);
    3. }
    And do your lerping from there.
     
  5. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Like this?
    Code (csharp):
    1. var target : Transform;
    2. var last : Transform;
    3. var accel = 2;
    4. static var lapsdone = 0;
    5. static var percent = 0.0;
    6. static var place = 0.0;
    7.  
    8. function FixedUpdate () {
    9.     if (Physics.Raycast (transform.position, Vector3.down, 1.5)) {
    10.         rigidbody.AddRelativeForce (Vector3.forward * 7);
    11.     }
    12.     if (target.collider) {
    13.         var targetPoint = target.position;
    14.         var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
    15.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
    16.         var look = targetPoint - transform.position;
    17.         look.y = 0;
    18.         targetRotation = transform.rotation.SetLookRotation(ZeroY(target.position));
    19.  
    20.     }
    21.     rigidbody.AddRelativeForce(Vector3.forward * accel);
    22.     var d1 = Vector3.Distance(transform.position, last.position);
    23.     var d2 = Vector3.Distance(transform.position, target.position);
    24.     var lerpVal = (d1/(d1+d2));
    25.     percent = Mathf.Lerp(last.GetComponent(Waypoints).progress, target.GetComponent(Waypoints).progress, lerpVal);
    26. }
    27.  
    28. function ZeroY(vector : Vector3) : Vector3 {
    29.    return new Vector3(vector.x, transform.position.y, vector.z);
    30. }
    That gives me an error on line 18, cannot convert void to UnityEngine.Quaternion
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    That's because I misread the Quaternion.SetLookRotation() docs. I'm not quite sure how to do it with the other functions, but I'll think about it and get back to you.
     
  7. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    Thank you for your help! n_n