Problem with gameobject pointing at mouse

I’ve ran into a problem.

The basic setup I have right now, two objects: my camera, and my player object.

The player moves via Transform on WASD, and is supposed to rotate on mouse movement.

The camera is top down (At a slight “3ps” style angle, which keeps the player object centre to the camera’s perspective, and rotates according to the players rotation.

This is the players movement script: using UnityEngine;using System.Collections;public class PlayerContro -

This is the players rotation script: using UnityEngine;using System.Collections;public class mouseLook : MonoBe -

The result of everything I have shown is it rotates, but it doesn’t rotate true to where the mouse physically is.

While I draw circles with the mouse, it will do full rotations, but not consistently point to where the mouse is. I’m not sure as to why.

The desired result is for the camera (child of player object) to follow the players movement and rotation, while the player moves with its movement script, and rotates to point true to where the mouse is.

Anyone got any ideas? Thanks in advance.

Edit: if it helps, the current rotation works like this.

drawing large circles with the mouse around the player gives a slower rotation, than extremely tight circles around the player.

Hi there!

I think one of the problems may be at you are not normalizing the vector you are capturing. In addition, I’m not sure why you set z=y and then y=0 and I don’t know what would cause it to rotate slower when the cursor is further away vs. closer. Would be interested in seeing a video.

With that being said, I had a similar problem to this a while back in another game. I needed an arm and a gun to rotate and point to where the mouse cursor was. The main difference in what I was doing and what you are trying to do, is that my guy’s arm was rotating around the z axis. Your character, since you are looking top down, is rotating around the y axis. Basically, this script:

  • Captures the vector between my character’ arm and the mouse position
  • Then normalizes the vector(captures direction only and always has a vector length of 1)
  • Takes difference of x and y from that vector and translates that to degrees
  • Finally, it rotates the objects position based on the degree calculated.

You should be able to use the below script and swap out z and y for whatever object you’re trying to rotate. Hope this helps!

public class ArmRotation : MonoBehaviour {
	public int rotationOffset = 90;
	// Update is called once per frame
	void Update () {

		Vector3 difference = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position; // subtracting the position of the player from the mouse position
		difference.Normalize ();   //normalizing the vector, meaning that all of the sum of the vector will be equal to 1
		float rotZ = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg; // find the angle in degrees
		transform.rotation = Quaternion.Euler (0f, 0f, rotZ + rotationOffset);