Topdown View, character always turning towards mouse position

Hello,

We have a little problem with the controls of our game. Its basically a 3rd Person game, but we also want to include a topdown view. For that we need the character to turn towards the mouse cursor.

I have found some script on the internet but it works not 100% the way we want. The pics below show the problem.

The character should turn to the mouse as in this example: (you have to scroll down to the „making a gun turret“ example and click on the preview) iTween for Unity by Bob Berkebile (pixelplacement)

Hope someone can help.

This is the script we use atm:

var mouse_pos : Vector3;
var target : Transform; //Assign to the object you want to rotate
var object_pos : Vector3;
var angle : float;


function Update ()
{
    mouse_pos = Input.mousePosition;
    mouse_pos.z = 5.23; //The distance between the camera and object
    object_pos = Camera.main.WorldToScreenPoint(target.position);
    mouse_pos.y = mouse_pos.y - object_pos.y;
    mouse_pos.x = mouse_pos.x - object_pos.x;
    angle = Mathf.Atan2(mouse_pos.x, mouse_pos.y) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(Vector3(0, angle, 0));
    
 
}



see if this helps. I think you might be over-complicating your problem.

thx for the tip, but i forgot to mention, that i also had tried the look at mouse script. problem was, that the charscter keeps turning and turning (hes spinning around), if i move the mouse to the left or right.

If these problem could be fixed the script would surely work.

display the values of

mouse_pos.y
object_pos.y
mouse_pos.x
object_pos.x

after statement 11 in your code

and then display

mouse_pos.y
mouse_pos.x
angle

before your transform.rotation statement and then you’ll be able to determine what your problem is

@ lockbox

Srry dont really understand what u mean (i’m not a programer). the values u mentioned are zero atm. i dont know which values i should insert there.

Well the solution is in the lookAtMouse.js script.

	// we get where the mouse hit a point in the world space. Hopefully the plane
	var hit : RaycastHit;
	var gotoPosition : Vector3;

	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
		gotoPosition = hit.point;
	}
	
	// now rotate toward the position.
	var rotate = Quaternion.LookRotation(gotoPosition - transform.position); 
	transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * rotateDamp);

But this can result in the Character looking down onto the ground itself.
That why in the lookAtMouse used a plane.

This could also result in the char spining if the mouse dont have a world position.
(over some gui or other stuff)
So maybe add a check for position is legal before turning.

Something to clarify the problem: If i use the Look at mouse script or the script above, the char doesnt stop rotating if i move the mouse left or right.

The goal is: place the mouse on the left/right side of the char, then the char rotates to that point, until the mouse cursor is directy in front of him. Then rotation should stop.

@snortop

Im guessing i have to combine our code with the lookatmouse right? where in the script should i insert it?

Your doing it backwards…

Your trying to get the position on the screen where the character is, figuring out an angle and then transferring it back to the rotation of the character… A far simpler method is to get a point on the plane of the character where the mouse is located, and face it. :wink:

And, since you gave some code… I will give you some code back.

var lerp : boolean = true;
var lookSpeed : float = 3.0;

function Update(){
	var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	var plane : Plane = new Plane(transform.up, transform.position);
	var dist : float = Vector3.Distance(Camera.main.transform.position, transform.position);
	plane.Raycast(ray, dist);
	
	var rotation = transform.rotation;
	transform.LookAt(ray.GetPoint(dist));
	if(lerp){
		transform.rotation = Quaternion.Slerp(rotation, transform.rotation, lookSpeed * Time.deltaTime);
	}
}

LookAtMouse.js from the wiki works great… if its not working, its because your camera script/setup is interfering somehow. If your camera is always set to be behind the character then it makes sense that he would infinitely rotate toward the mouse cursor, because as the player and camera rotate they’ll never actually face the mouse if you keep it in the same screen position. I’d try it using one of the built in scripts like SmoothFollow or SmoothLookAt to test.

Well i tested it on a capsule.
The lookAtMouse script… and work great.

See…

The mouse rotate toward the mouse position.

but it more sound you also want it to move towards position.

@ BigMisterB

I tried ur script but i get a compiler error:

Assets/Scripts/TopDownScripts/TurnTowardsMouseCursor.js(7,44): BCE0017: The best overload for the method ‘UnityEngine.Vector3.Distance(UnityEngine.Vector3, UnityEngine.Vector3)’ is not compatible with the argument list ‘(UnityEngine.Vector3, UnityEngine.Transform)’.

yes, the camera is indeed behind the character. then ill have to test another cam setup.

I tested the lookatmouse with the mouse orbit and it works now, only problem now is, that the character is always centererd on the screen. How can i manage it, that he is on the lower side of the screen, as on the pics above?

parent a pivot point to the character and position it with whatever offset you need, and have the camera script target that, not the character. Or you might need to parent the character to the pivot, not sure without playing with it myself.