Limit the Distance of transform.Translate

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

So just to clarify:
This aim object is supposed to always be at the same distance to the player, basically a cylinder around him, and he can move the aim all along that cylinder?

In that case, you can add an empty gameobject to the player and add the aim object as a child of that empty game object. Position the aim object in the distance you want it to be and then you can move the aim object by turning the empty game object around the y achsis, the aim object will follow.

Thanks for your reply. The Distance of the aim object should not be fixed. So basically there should be no minimum distance only a maximum distance. (An example would be the whistle from the Pikmin series).

After some further testing I realized there are some more issues with my code. If I parent the aim object to the player, the position is relativ to the player (imho it would be better if i push the joystick upwards it should always be up). If I don’t parent the aim object I can “loose” it on the map.

If you do it the way I described, you can easily adjust the distance as well. Basically, you could work with cylinder coordinates instead of euler coordinates, so you only have an angle and a distance to the origin to deal with. You can also move it up and down the y axis, if need be.

I’ll try to implement it that way tomorrow (I’m done for today). I hope I understand everything correctly but your description sounds promising :slight_smile:

If you need some help, just ping me here. Good luck.