Weird Movement Issue/ Scripting Question.

I’ve been lurking these forums for a good while now and normally I can find my answer of at least something close enough but sadly I can not for this issue.

So the concept we are using for movement is sorta like a “string” the player can moves their aiming reticule along X and Y. The player’s “Body” follows behind where every they aim but a slightly slower pace, The camera itself follows the player and has a smaller “movement box” and is confined to stay in it and will “tilt” with the player.

The issue I am having is that I assumed I could just have the player look at the aiming reticule and follow with a Lookat but the issue is while it moves great and does what I want (IE follows the aiming reticule fine and rotates towards it) it moves along X,Y fine but some reason it gets pushed back along Z, so it seems to get further and further away from the aiming reticule. Though while typing this I think I might have to program out the movement exactly instead of using Lookat.

The Scripting question I have is… I want to confine the player, aiming reticule, and camera in a “movement box” which I guess I mean by that is an area they are allowed to move in that isn’t dictated by the edges of the screen. my problem is I just don’t know how to go about stopping them from passing the edges of my box with allowing full movement. My example is if I move left to the edge of the box I want the player/Aiming reticule to no longer be allowed to move left anymore but still have full motion to go right, up, down. What seems to have caused me the most issue is trying to restrict the player controlled movement of the aiming reticule. I can get it to stop but it won’t let me move left/right anymore… Any help with this would be amazingly helpful I’ll include the code of what I have for the aiming reticule (though my if’s aren’t filled out)

using UnityEngine;
using System.Collections;

public class Aiming : MonoBehaviour 
{
	//movement
	public float aimingSpeed;
	public float movingFoward;
	private float amtToMoveFoward;
	
	//aiming box
	private Vector3 aimingBoxLeft = new Vector3(-15,0,0);
	private Vector3 aimingBoxRight = new Vector3(15,0,0);
	private Vector3 aimingBoxUp = new Vector3(0,15,0);
	private Vector3 aimingBoxDown = new Vector3(0,-1,0);

	void Start () 
	{
		
	
	}
	
	void Update () 
	{
		//movement method
		AimingMovement();
		
	}
	
	void AimingMovement()
	{
		//movement
		amtToMoveFoward = movingFoward * Time.deltaTime;
		float amtToMove = Input.GetAxisRaw("Horizontal") * aimingSpeed * Time.deltaTime;
		float amtToMoveUp = Input.GetAxisRaw("Vertical") * aimingSpeed * Time.deltaTime;
		transform.Translate(Vector3.right * amtToMove);
		transform.Translate(Vector3.up * amtToMoveUp);
		transform.Translate(Vector3.forward * amtToMoveFoward);
		
		//Box Check
		if (transform.position.x < aimingBoxLeft)
		{
			
		}
		if (transform.position.x > aimingBoxRight)
		{
			
		}
		if (transform.position.y < aimingBoxDown)
		{
			
		}
		if (transform.position.y > aimingBoxUp)
		{
			
		}
	}
	
}

Typically, a reticle works in screen space, so the first thing is to clamp the reticle position to the screen rectangle you want to confine it to. This is just a matter of using Mathf.Clamp on the X and Y mouse coordinates individually. If you want the target point to be at a fixed distance in front of the camera then you can use Camera.ScreenToWorldPoint. Use the clamped reticle XY position as the XY coordinates of the screen point and set the Z to be the distance from the camera that you want the target point to appear at:-

var retX = Mathf.Clamp(Input.mousePosition.x, minX, maxX);
var retY = Mathf.Clamp(Input.mousePosition.y, minY, maxY);
var targetPoint = Camera.main.ScreenToWorldPoint(new Vector3(retX, retY, targetDistance));

Thank you so much for your reply. I don’t think I was completely clear; as I guess I made it sound like a FPS haha. The aiming reticule is actually controlled by arrow keys. (currently) I was able to to actually solve it (though I doubt it is an optimized solution) but I do thank you greatly for explaining something I’ll eventually have to learn.

What seems to be giving me the most trouble is dealing with rotation. The idea is to mimic starfox 64 controls. So the problem I’m having is when I try to tilt my game object in the way of the player is aiming I can do it with things like Lookat or programing in the rotation with the movement but when I do my game object ends position ends up getting change (mostly in Z) and will cause the player to be forced backwards away from it (as in it is actually changing the position of Z). If I constantly move the reticule it will cause the game object to orbit around one point. I assume its a matter of just getting a grasp of Slerp, lerp, and trying to get a better handle on Quaternions. Any direction would be greatly appreciated (I guess I’m saying I don’t need a whole code laid out for me just direction or maybe an explanation if that is easier) Thank you for taking time for reading and trying to help