- Home /
my ai wiggles slightly
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public Vector2 lastKnownLocation;
public Vector2 lastSetLocation;
public Vector2 currentTarget;
public float desiredRotation = 0;
public float curRotation = 0;
public float amountToRotate = 0;
private Vector3 EulerRotation;
void Start()
{
lastKnownLocation = transform.position;
currentTarget = new Vector2(Random.Range(-150f, 150f), Random.Range(-100f, 100f));
}
void Update()
{
if ((currentTarget - lastKnownLocation).sqrMagnitude <= 9)
{
currentTarget = new Vector2(Random.Range(-150f, 150f), Random.Range(-100f, 100f));
}
desiredRotation = Vector2.SignedAngle(currentTarget - lastKnownLocation, transform.right);
amountToRotate = 70 * Time.deltaTime;
if (Mathf.Abs(desiredRotation) <= amountToRotate)
{
curRotation = desiredRotation + curRotation;
}
else
{
curRotation -= Mathf.Sign(desiredRotation) * amountToRotate;
}
if (curRotation > 180)
{
curRotation -= 360;
}
else if (curRotation < -180)
{
curRotation += 360;
}
EulerRotation = Quaternion.Euler(new Vector3(0f, 0f, curRotation)) * Vector3.right;
lastKnownLocation = transform.position + (EulerRotation * 25 * Time.deltaTime);
transform.eulerAngles = new Vector3(0, 0, curRotation);
transform.position = lastKnownLocation;
}
}
in my above code i made a simple test bed for my ai to try to figure out why it wiggles. in this test bed it doesn't wiggle quite so much as in my actual game but, it still wiggles slightly. can anyone provide reason as to why it wiggles, and maybe a possible solution.
Edit: I found out the reason why it wiggles more on my game than it does on my test bed. It's because most of this code is actually in fixed update on my game increasing the wiggle range from about 1.1 - 1.4 degrees positive and negative
I havn't tested your script, but I find this line odd :
lastKnownLocation = transform.position + (EulerRotation * 25 * Time.deltaTime);
Why is there a rotation value in your position value? Since your calculate rotation in update and use it in your position, my guess is this is the source of the wiggle
that rotation is rotating the amount i'm moving if it wasn't there the ai would forever drive straight the transform.eulerangles happens after i change the position because in my actual game there is this whole check for obstacles function that happens between them sorry if this was confusing
it's the same value as transform.right would be it's not the cause of the wiggle i actually just checked to confirm and yeah the wiggle is still present
I justed tested your script. With smaller values overall I have no wiggle at all (with larger values, it goes way to fast for me to see).
void Start()
{
lastKnownLocation = transform.position;
currentTarget = new Vector2(Random.Range(-2f, 2f), Random.Range(-3f, 3f));
}
void Update()
{
if ((currentTarget - lastKnownLocation).sqr$$anonymous$$agnitude <= 0.5F)
{
currentTarget = new Vector2(Random.Range(-2f, 2f), Random.Range(-3f, 3f));
}
[...]
lastKnownLocation = transform.position + (EulerRotation * 2 * Time.deltaTime);
In your script if you reduce the speed do you still see the wiggle ?
sorry i should have mentioned that in my game the pixels per unit is set to 1 and the camera is set to 114
but either way even if you reduce the speed the wiggle is still there it's only about plus or $$anonymous$$us 2 degrees so it's not much of a wiggle but it's enough that he's crashing in my game where he really shouldn't if you watch the vehicles desired rotation in the inspector you'll notice that when it finally reaches 0 the desired rotation starts jumping plus or $$anonymous$$us about 2 degrees
Answer by Dreamerking007 · Aug 16, 2020 at 11:29 AM
i discovered the problem. the problem was that in the first if statement it should have been curRotation - desiredRotation if i change this
if (Mathf.Abs(desiredRotation) <= amountToRotate)
{
curRotation = desiredRotation + curRotation;
}
else
{
curRotation -= Mathf.Sign(desiredRotation) * amountToRotate;
}
into this
if (Mathf.Abs(desiredRotation) <= amountToRotate)
{
amountToRotate = Mathf.Abs(desiredRotation);
}
curRotation -= Mathf.Sign(desiredRotation) * amountToRotate;
the problem is fixed and the wiggle becomes almost non-existent
Your answer
Follow this Question
Related Questions
Enemy Rotation AI 0 Answers
Unity AI Obstacle Avoidance Help 3 Answers
2D AI: How to get the AI to aim ahead of its target? 3 Answers
Spinning 360 degrees and stopping (AI) 1 Answer
Slerp rotation on collision - C# 2 Answers