- Home /
Sprite "shudders" on arrival
So I have a random movement script for a monkey and it's going from place to place fine. But sometimes! it will shake like crazy when it arrives at a specific point. But it's not consistent. Here is the script and a quick video of the issue.
[SerializeField]
private float _speed = 32f;
private float _closeThreshold = .1f;
[SerializeField]
private Transform[] _treeTopLocations;
//The position we want to move to
private Vector3 _targetPos;
void Start()
{
//Set our first target location to where we already are
_targetPos = transform.position;
StartCoroutine(MonkeyMovement());
}
void Update()
{
//If we aren't close to our target location
if (Vector3.Distance(transform.position, _targetPos) > _closeThreshold)
{
//Find the direction from where we are to where we want to be
//Normalize it to get a direction rather than the whole distance
Vector3 _currentDir = (_targetPos - transform.position).normalized;
//Move towards the target
transform.Translate(_currentDir * _speed * Time.deltaTime);
}
}
IEnumerator MonkeyMovement()
{
while (GameManager.gameManager.gameOver == false)
{
if (UIManager.uIManager.pauseMenuVisible == true)
{
yield return null;
}
else
{
//Dont get a new target for 2 seconds
yield return new WaitForSeconds(2);
//Get a new target
Transform newTree = TreeTopLocations();
_targetPos = newTree.position;
}
}
}
public Transform TreeTopLocations()
{
int randomIndex = Random.Range(0, _treeTopLocations.Length);
return _treeTopLocations[randomIndex];
}
Answer by revolute · Sep 25, 2019 at 06:00 AM
There are a few cases that could be the problem.
The first one would be _closeThreshold = .1f not enough to cover the delta movement of speed 32. 32 * 0.02 (or 0.0166666..) = 0.64 (or 0.5). This is mostly likely in your case.
Next would be the movement being translated in wrong space. Translate does the vector movement in local space unless specified to world space. Since other positions are not being done wrong, I doubt this.
Try raising the threshold to 1 and see if that solves it.
hey revolute, just saw your answer I will test this out. I am very sorry for the delay in seeing this. thanks for your time!