• 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 /
  • Help Room /
avatar image
0
Question by Theobroma · Nov 17, 2020 at 09:48 PM · scripting beginnerquaternions

Quaternion Angle

Hello,

I am new to Unity and a beginner at programming. I am playing around with Unity to create an RTS control scheme. The script is supposed to detect when the player right-clicks a position, after which the player's unit should rotate to face in the direction of the click, and once it has finished rotating, it begins moving towards it. After following several tutorials and searching forum posts, I have been able to implement it through a Quaternion.Slerp within a coroutine loop. Problem is, once the ship is facing the correct direction, it doesn't move.

Here is the code:

 using System.Collections;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class PlayerController : MonoBehaviour
 {
     public Camera cam;
     public NavMeshAgent agent;  
 
     [SerializeField]
     private float rotationSpeed = 0.3f;
 
     private Vector3 moveOrder;
     private Quaternion targetDirection = default;
     private RaycastHit hit;
     private bool isRotating;
 
     private void Start()
     {
         agent.updateRotation = false;
     }
 
     // Update is called once per frame
     void Update() 
     {
         if (Input.GetMouseButtonDown(1))
         {            
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
             {
                 StartCoroutine(rotateShip(transform.rotation, targetDirection));
                 if (isRotating == false)
                 {
                     agent.SetDestination(hit.point);
                 }   
             }
         }
     }
     IEnumerator rotateShip(Quaternion currentRotation, Quaternion targetDirection)
     {
         isRotating = true;
         
         while (currentRotation != targetDirection)
         {
             targetDirection = Quaternion.LookRotation(hit.point - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, targetDirection, rotationSpeed * Time.deltaTime);
             yield return 1;
         }
         isRotating = false;
     }
 }

The script seems to be stuck in the coroutine loop because the while loop condition is never met (i.e. currentrotation != targetRoation).

I saw this tutorial for calculating angles in unity, and tried my hand at implementing something similar: calculate the angle using the Inverse Tangent as shown in the video (except using the y axis for rotation instead of z, since my game uses 3D coordinates), then change the condition of the loop so it stops once the angle is acute enough. The code didn't work, it would always output a negative right angle "-90." I also tried my hand at Quaternion.Angle, but I couldn't wrap my head around it.

I would appreciate if someone could shed some light into how to go about solving this. I am not just interested in a solution but in developing the thinking process to troubleshoot this sort of thing myself in the future. Thank you in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by xxmariofer · Nov 18, 2020 at 10:28 AM

you are using Slerp wrong

 using System.Collections;
 using UnityEngine;
 using UnityEngine.AI;
 
 public class PlayerController : MonoBehaviour
 {
     public Camera cam;
     public NavMeshAgent agent;  
 
     [SerializeField]
     private float rotationSpeed = 0.3f;
 
     private Vector3 moveOrder;
     private Quaternion targetDirection = default;
     private RaycastHit hit;
     private bool isRotating;
 
     private void Start()
     {
         agent.updateRotation = false;
     }
 
     // Update is called once per frame
     void Update() 
     {
         if (Input.GetMouseButtonDown(1))
         {            
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
             {
                 StartCoroutine(rotateShip(transform.rotation, targetDirection));
             }
         }
     }

      IEnumerator rotateShip(Quaternion currentRotation, Quaternion targetDirection)
      {
          isRotating = true;
          float amount = 0;
          while (currentRotation != targetDirection)
          {
              targetDirection = Quaternion.LookRotation(hit.point - transform.position);
              transform.rotation = Quaternion.Slerp(transform.rotation, targetDirection, amount);
              amount += rotationSpeed * Time.deltaTime;
              yield return null;
          }

          isRotating = false;
          agent.SetDestination(hit.point);
      }
Comment
Add comment · Show 4 · 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
avatar image Theobroma · Nov 18, 2020 at 06:23 PM 0
Share

Thanks for answering.

Unfortunately that didn't work as intended. It will do the first rotation, but any additional clicks will just snap it to a new direction.

$$anonymous$$y guess is that this condition is still not being met: (currentRotation != targetDirection)

So it never resets the "Amount" variable to zero and it keeps adding itself (so it "snaps").

After reading a little, that condition might not be a good idea because of floating point inaccuracy, so it is never quite matching, but i have no idea what other conditions would be good.

avatar image xxmariofer Theobroma · Nov 18, 2020 at 06:38 PM 1
Share

I have just seen i had copied the method twice (your version and mine) please make sure the code you tested is the updated in the previous answer

avatar image Theobroma xxmariofer · Nov 18, 2020 at 08:15 PM 0
Share

I did see that! Thanks for the heads up, regardless!

Show more comments
avatar image
0

Answer by Theobroma · Nov 26, 2020 at 04:41 AM

For future reference, I was able to solve it. The Code ended up being the following:

   void Update()
      {
          if (Input.GetMouseButtonDown(1))
          {
              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
              Plane plane = new Plane(Vector3.up, new Vector3(0f, transform.position.y, 0));
              float distanceToPlane;
              if (plane.Raycast(ray, out distanceToPlane))
              {                
                  moveOrder = ray.GetPoint(distanceToPlane);
                  targetDirection = Quaternion.LookRotation(moveOrder - transform.position);
                  StartCoroutine(rotateShip(transform.rotation, targetDirection));
              }
  
          }
      }
  
      IEnumerator rotateShip(Quaternion currentRotation, Quaternion targetDirection)
      {
          while (Quaternion.Angle(targetDirection, transform.rotation) > 1f)
          {
              targetDirection = Quaternion.LookRotation(moveOrder - transform.position);
              transform.rotation = Quaternion.Slerp(transform.rotation, targetDirection, rotationSpeed * Time.deltaTime);
              yield return null;
          }
          agent.SetDestination(moveOrder);
      }

This code has several of adjustments from my original code.

  1. The first one is the use of of a plane to shoot the ray at. Because the ground is lower than the GameObject, it was trying to orient itself towards the ground constantly, causing some wiggling. By creating a plane at the same height as the ship, this is no longer a problem:

Plane plane = new Plane(Vector3.up, new Vector3(0f, transform.position.y, 0));

  1. The second issue, and the biggest, was floating point inaccuracy. Because computers always have some inaccuracies in comparing floating point calculations, it was always returning false when comparing two quaternion rotations, even though at a glance they seemed identical. To solve this, I used the while (Quaternion.Angle(targetDirection, transform.rotation) > 1f) which simply measures the angle between both rotations and, if it's within 1 degree, it considers it good enough.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

222 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Child object looktowards waypoint 0 Answers

Turning the camera 0 Answers

Get model of circle with specyfic angle 1 Answer

How to enable and disable back game object for ui button after a few seconds? 1 Answer

Particles that follow the Player 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges