Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Harizio · Aug 01, 2014 at 02:12 PM · rotationtransformlerpeuleranglesdegrees

How to rotate 90 degrees from 270 degrees to 0 with lerp? c#

Hi. Im making a small 2d game (that im editing in 3d) and Im currently working on making simple paths that guards can circle in. Im using waypoint-triggers that check the guard for the movement script, set its speed to 0, rotates the guard the desired amount, and sets the guard moving again.

Everything else seems to work fine with my paths, but whenever a waypoint rotates the guard from 270 degrees to 0 degrees, the guard turns the full 270 degrees the long way, instead of going 90 degrees the shorter way. The odd thing about this is, that I can turn the guard from 0 degrees to 270 degrees, and the guard does it the short way with 90 degrees.Any ideas on why is this happening and how to fix it?

Ill post my 2 scripts here: the Waypoint.cs is for the waypoint triggers that give the guard directions, and the WMove.cs is for the guard.

Waypoint.cs

 using UnityEngine;
 using System.Collections;
 
 public class Waypoint : MonoBehaviour {
     public float waitbefore = 0f;
     public float rotate = 0f;
     public float waitafter = 0f;
     public float setforwardspeed = 0f;
     public float setsidespeed = 0f;
 
     public GameObject Mover;
 
     public Vector3 settargetAngles; //rotate
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         }
 
     void OnTriggerEnter(Collider other) 
         {
             if (other.GetComponent<WMove>() != null) {
             Mover = other.gameObject;
             WMove WM = other.GetComponent<WMove>();
             WM.forwardspeed = 0;
             WM.sidespeed = 0;
             Debug.Log (other.gameObject.name);
             StartCoroutine(corowaitbf());
             }
     
     }
     IEnumerator corowaitbf(){
         yield return new WaitForSeconds(waitbefore);
         Debug.Log ("Waitbefore completed!");
         WMove WM = Mover.GetComponent<WMove> ();
         settargetAngles.z = Mover.transform.eulerAngles.z + rotate;
         WM.targetAngles = (settargetAngles);
         yield return new WaitForSeconds (waitafter + 3f);
         Debug.Log ("Waitafter completed");
         WM.forwardspeed = setforwardspeed;
         WM.sidespeed = setsidespeed;
 }
 }

WMove.cs

 using UnityEngine;
 using System.Collections;
 
 public class WMove : MonoBehaviour {
     public float forwardspeed = 0f;
     public float sidespeed = 0f;
     public Vector3 targetAngles; //rotate
     public float smooth = 3f;  //rotate
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         CharacterController cc = GetComponent<CharacterController> ();
         Vector2 speed = new Vector2 (forwardspeed, sidespeed);
         speed = transform.rotation * speed;
         cc.Move( speed * Time.deltaTime );
         //rotate
         transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAngles, smooth * Time.deltaTime);
         if (targetAngles.z < 0) {
             targetAngles.z += 360;        
         }
         if (targetAngles.z >= 360) {
             targetAngles.z -= 360;    
         }
         if (targetAngles.z - transform.eulerAngles.z > -1 && targetAngles.z - transform.eulerAngles.z < 1) {
             //transform.eulerAngles.z = targetAngles.z;
             Vector3 temp = transform.eulerAngles;
             temp.z = targetAngles.z;
             transform.eulerAngles = temp;
 
             //transform.rotation.z = targetAngles.z;
         }
         //if (transform.eulerAngles = targetAngles) {
         //    new Vector3 targetAngles     
         //}
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 01, 2014 at 02:44 PM

Euler angles and Lerp() are always literal. If you tell them to go from 0 to 270, then mathematically they walk from 0 to 270. The solution is to give the angles a mathematical pathway that does what you want. In your case, find the distance from the start angle to the end angle, and if it is greater than 180 degrees, adjust the end angle by either add or subtracting 360. So in the case of 0 to 270, it becomes 360 to 270.

Note my typical solution to this kind of problem is to use Quaternions rather than eulerAngles. Quaternion rotations are always lazy and take the shortest path between two rotations. While it takes some rampup to understand Quaternions, the resulting code is usually shorter and simpler.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Problem about Rotating the Character 1 Answer

How do you smoothly transition/Lerp into a new rotation? 3 Answers

eulerAngles.y always returns 179.9999 1 Answer

Worldspace EulerAngle.x regardless of z rotation ? 3 Answers

Unit rotation fails consistently on all slerp rotations after the first? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges