- Home /
Can't get "rotate to mouse" script to work?
I'm working on a top-down game, where I want the player to slowly rotate towards the mouse, like in most top-down games (Like in "Alien Swarm" for example).
Right now I got this script:
using UnityEngine;
using System.Collections;
public class sPlayerRotation : MonoBehaviour {
public GameObject MainCamera;
public float turnSpeed;
void Start () {
turnSpeed = 2;
MainCamera = GameObject.Find("MainCamera");
}
void Update ()
{
Vector3 MouseWorldPosition = MainCamera.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
transform.LookAt(MouseWorldPosition);
transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0));
}
}
All this does is to rotate me instantly to the center of the map, which is not what i want. I want it to slowly rotate it to the mouse (along the y axis) How would I do so?
Also, the camera object is called "MainCamera" and have the tag "MainCamera"
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613064342im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate Cube towards Mouse position 2 Answers
unity2D: make object face mouse 2 Answers
Why Rotate player toward mouse direction keeps increasing rotation speed? 1 Answer
Mouse rotation, jerky movement 1 Answer
Mouse Problem 2 Answers