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!
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);
}
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.
I have just seen i had copied the method twice (your version and $$anonymous$$e) please make sure the code you tested is the updated in the previous answer
I did see that! Thanks for the heads up, regardless!
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.
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));
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.
Your answer
Follow this Question
Related Questions
Trigger script function from ARKit UnityARUtility class 0 Answers
Floating scifi display to show results from database query 0 Answers
Having some doubts on the basics of scripting,A little doubt about some terminology 0 Answers
Adding prefab objects to a list on another script and object? 2 Answers
How do i find out if teleport was unsuccessful? (VR VIVE) 0 Answers