Rotate camera and player with mouse 2d top-down shooter in C#
Hi guys,so here is the problem i would need help with.I make a topdown shooter and the idea is,if you move the mouse the camera and player rotates,so basically the player always looks in the same direction,and the ground "rotates".I have already did that part when you rotate the camera in inspector(z axis) the player follows, but i cant write a proper function to rotate the camera with mouse.
Here is my code:
Player:
void Update()
{
var CharacterRotation = Gamecam.transform.rotation;
CharacterRotation.x = 0;
CharacterRotation.y = 0;
transform.rotation = CharacterRotation;
}
Camera(after hours of searching in forums,still not working properly):
public GameObject player;
private Vector3 offset;
public Vector3 playerPos;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
private void Update()
{
// Get Angle in Radians
float AngleRad = Mathf.Atan2(Input.mousePosition.y - transform.position.y, Input.mousePosition.x - transform.position.x);
// Get Angle in Degrees
float AngleDeg = (180 / Mathf.PI) * AngleRad;
// Rotate Object
this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg * 2);
}
// Update is called once per frame
void LateUpdate () {
transform.position = player.transform.position + offset;
}
Answer by Spartan691 · Nov 18, 2020 at 04:15 PM
I come late for this post,but where u put float AngleRad = Mathf.Atan2(Input.mousePosition.y - transform.position.y, Input.mousePosition.x - transform.position.x);, u should add "Mathf.Rad2Deg".
Just like this:
float AngleRad = Mathf.Atan2(Input.mousePosition.y - transform.position.y, Input.mousePosition.x - transform.position.x)*Mathf.Rad2Deg;
And in LateUpdate() transform.rotation= Quaternion.Euler(0,0,AngleDeg*rotationSpeed);
3 years late but maybe helps somebody else xD.
Your answer
Follow this Question
Related Questions
Top Down Camera View Problem 1 Answer
How I can make my camera keep a distance between two objects? 0 Answers
Set the rotation axis of the camera 0 Answers
How to do a wall jump? i am very novice 0 Answers
Issue with Rigidbody2D (I think) 2 Answers