Pan Movement for 3rd view cam

Hi,

I’m doing a pan movement with the middle mouse click.

I’ve got a camera wich rotate around a target linked by the rotate script.

At the moment I tried that

public class Pan : MonoBehaviour {

    public Transform Target;
    public float Sensitivity;

    private float MoveX;
    private float MoveY;
	
	// Update is called once per frame
	void Update () {

        if (Input.GetMouseButton(2))
        {
           MoveX += Input.GetAxis("Mouse X") * Sensitivity;
           MoveY -= Input.GetAxis("Mouse Y") * Sensitivity;
                       
        }

        Target.position = new Vector3(MoveX, 0, MoveY); 


	}
}

Obviously the pan movement is always in the same direction, so basicaly now I need to connect it with the direction of the camera, do someone has an idea for that ?

I’ll continue to post my researches an problems here.

Cheers for the help

BooBi

New code:

   public Transform Target;
    public Transform Camera;
    public float Sensitivity;

    private Vector3 moveDirection = Vector3.zero;
    private float MoveX;
    private float MoveY;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        
        //Target.rotation = Quaternion.Euler(0, Camera.rotation.y, 0);
        Target.rotation = Camera.rotation;

        if (Input.GetMouseButton(2))
        {
           MoveX += Input.GetAxis("Mouse X") * Sensitivity;
           MoveY -= Input.GetAxis("Mouse Y") * Sensitivity;
                     
        }

        moveDirection = new Vector3(MoveX, 0, MoveY);
        moveDirection = transform.TransformDirection(moveDirection);
        Target.position = moveDirection;
             

	}
}

if I’m using this line

Target.rotation = Quaternion.Euler(0, Camera.rotation.y, 0);

it doesn’t work (the target doesn’t rotate at all.

if i’m using this line instead

Target.rotation = Camera.rotation;

The cube as the same rotation as my camera and the pan works better but I only need the Y rotation not the others otherwise when I pan forward i go through the ground the target can only move on 2D. And bigger problem, now when I’m rotating the cam with left click, the cube move slightly.

So I need to constraint my target Y rotation to the Camera Y rotation and then move in the right direction.

I was sure I did the constraint before, so now I constrainted the rotation with a Lookat in Y.

and I found that the problems come from that line:

moveDirection = transform.TransformDirection(moveDirection);

I would need to do move Transform.forward but how to implement that in my script ?

done with that

transform.Translate(transform.right * -Input.GetAxis("Mouse X") * Sensitivity, Space.World);       
transform.Translate(transform.forward * -Input.GetAxis("Mouse Y") * Sensitivity, Space.World);