Hello Everyone.
I’ve been struggling with a probably stupid issue and hope to find some help.
I try to move a GameObject (an aim controlled with the right joystick) around my player. The aim should be limited to a specific distance of the player.
I managed to limit the maximum Distance of the aimGOto the playerGO but the way I did it feels completely wrong.
Here is my current “Limitation” Code:
float targetDistance = Vector3.Distance(transform.position, targetAim.transform.position);
if (targetDistance > maxAimDistance) {
if (Mathf.Abs(targetAim.transform.position.x - transform.position.x) > maxAimDistance) {
targetAim.transform.position = new Vector3(originalX, targetAim.transform.position.y, targetAim.transform.position.z);
}
if (Mathf.Abs(targetAim.transform.position.z - transform.position.z) > maxAimDistance) {
targetAim.transform.position = new Vector3(targetAim.transform.position.x, targetAim.transform.position.y, originalZ);
}
}
Code for Moving the aimGO:
Vector3 aimDirection = new Vector3(inputAim.x, inputAim.y, 0.0f);
if (aimDirection.magnitude > 0.0f) {
targetAim.SetActive(true);
aimDirection.Normalize();
targetAim.transform.Translate(aimDirection * Time.deltaTime * aimingSpeed);
} else {
targetAim.SetActive(false);
}
Is there a better way to limit the Tranlate function?
I also tried to use Vector3.MoveTowards instead of transform.Translate among others because the maxDistanceDelta param looked promising but in the end wasn’t what I was looking for (or I used it the wrong way).
Another problem with my solution is that the the aim is now constrained to a rectangle around the player instead of a circle.
Thanks for reading my question