Mouse orbit + ScreenToWorldPoint (LookAt) issues.

For those that are in the know - Line 36 might contain my problem.

Hey guys. I’ve been having a bit of trouble for the better part of the day with a control I’m trying to implement. I have a 3rd person camera set up, using a Orbit around the player, I’m trying to use a static cursor position (roughly centered on screen - and when I say static cursor position I’m talking about the 2D GUI AIMING texture, not the mouse… so I might be using wrong terms here.) and use ScreenToWorldPoint to get a position for my character to rotate to.

The reason for this is when the player draws his gun, and only then, the GUI2D Texture appears, and then from there I’m trying to get the 3D space for him to look at. I’ve tried to restrict the up/down motion and only have him face in the direction, but I can’t even seem to get this far.

What current happens is the player warps to some odd ball angle ( I’m assuming because I’m not getting the correct position I want) and also he still changes in axis’ that I thought I limited. The player also for the most part warps to face the screen…

    	//Aimer
    	public Texture2D aim;
    	//Link to character controller to determine if weapon is out
    	public ThirdPersonController WpnOut;
    	//Store if weapon is out, and update it.
    	private bool isWeaponOut;
    	//Aimer size up and down
    	private int aimW = 32;
    	private int aimH = 32;
    	//floats for storing mouse cords to make player look at (might change to int)
    	private float mouseX;
    	private float mouseY;
    	//character ref so we can make him face the mouse postiion
    	public GameObject player;
    	//Storage of the variable that character will look at
    	private Vector3 posToFace;
    	//make sure our character only looks along the correct axis
    	private float justTurn;
    	
    	
    	
    	void Start () {
    		//ref to the AI script attacked for checking if weapon is out - if it is, over-ride current animations and use gun drawn animations.
    		isWeaponOut = WpnOut.wpnDrawn;
    		
    		justTurn = camera.transform.position.y - player.transform.position.y;
    	}
    	
    	void Update()
    	{
    		isWeaponOut = WpnOut.wpnDrawn;
    		//Debug.Log (isWeaponOut);	
    		
    		if(isWeaponOut)
    		{	
//Right here might be my issue.  How can I use the GUI 2D texture to project what I need to look at straight away from the camera?
    			mouseX = Input.mousePosition.x;
    			mouseY = Input.mousePosition.y;
    			posToFace = camera.ScreenToWorldPoint(new Vector3(mouseX,mouseY,justTurn));
    			player.transform.LookAt(posToFace);	
    		}
    	}
    
    	void OnGUI()
    	{
    		if (isWeaponOut){
    			//Static method.
    			GUI.DrawTexture(new Rect(Screen.width/2 + 200,Screen.height/2,aimW,aimH), aim);
    			//Non-static aim'er following mouse.
    			//GUI.DrawTexture(new Rect(Input.mousePosition.x -16 ,Screen.height - Input.mousePosition.y -16, aimW, aimH), aim);
    		}
    		else{return;}

I’m not sure to have understood what you wanted and I’m not a specialist, but…

	void Update () {
		Vector3 posToFace = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Mathf.Max(camera.nearClipPlane,Vector3.Distance (player.transform.position,camera.transform.position)-1.0f)));

		player.transform.LookAt(posToFace);
		player.transform.localRotation = Quaternion.Euler(new Vector3(0.0f, player.transform.localRotation.eulerAngles.y, 0.0f));

	}

The Update() function take the mousePosition variable, then pass it to the ScreenToWorldPoint() function (the Z position is the distance from the camera to the player - 1.0f).
This way, you have position of the mouse, in 3D space (the Z position being between the camera and the player).

Next, the player look at this 3D position (through LookAt()). Then, the rotation is blocked on X and Z axis. This allows the player to only turn on itself.