Problems with rotation 2D
Hi , I have a problem with my rotation script , I am making a top down shooter and can you please tell me why it is not working properly. The player should face the mouse but it is not doing it properly . example: my mouse is in the left part of the screen and the player is faceing up. here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour {
public float speed = 5f;
// Update is called once per frame
private void Update () {
Vector2 directie = Camera.main.ScreenToWorldPoint (Input.mousePosition) - transform.position;
float angle = Mathf.Atan2 (directie.y, directie.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis (angle, Vector3.forward);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, speed * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
2D top down shooter can only aim to the top right (mouse aim) 0 Answers
How can I rotate a Parent GameObject so a Child points towards the cursor? 0 Answers
How can I make my player's gun point at the cursor, not the player itself? 1 Answer
How to set animations for a top down shooter? How to change the animations by the player rotation? 0 Answers
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers