- Home /
Character not rotating
I have this script on my character
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerMovement : NetworkBehaviour {
private Rigidbody2D rb2d;
private float moveSpeed;
private Camera cam;
void Start ()
{
if (isLocalPlayer)
{
cam = this.transform.GetChild (0).GetComponent<Camera> ();
cam.enabled = true;
}
rb2d = gameObject.GetComponent<Rigidbody2D> ();
moveSpeed = 100.0f;
}
void Update ()
{
float horizontal = Input.GetAxis ("Horizontal") * moveSpeed;
float vertical = Input.GetAxis ("Vertical") * moveSpeed;
rb2d.AddForce (Vector2.right * horizontal);
rb2d.AddForce (Vector2.up * vertical);
Vector2 mousePos = cam.ScreenToWorldPoint (new Vector2 (Input.mousePosition.x, Input.mousePosition.y));
float AngleRad = Mathf.Atan2 (mousePos.y - transform.position.y, mousePos.x - transform.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
rb2d.rotation = angle;
}
}
the script works okay except for the last part that deals with rotating the character, the character wont rotate to follow the mouse pointer, the strange thing is that this script has worked before (albeit on older versions of unity) but has chosen to break now,
What do I need to do to get the character following the mouse pointer?
here is the characters components in the inspector if that helps.
Answer by FM-Productions · Jun 03, 2017 at 09:28 PM
You should rotate the gameObject itself, not the rigidbody. Also the rotation is generally a Vector3 value (x, y, z-value). You try to assign a single float to the rotation, not a Vector value. Since you have a 2D game, I guess the axis you want to rotate around is the z-axis and your visible plane is the x and y-axis?
Then simply replace line 37 with this:
transform.rotation = new Vector3(0, 0, angle);
I replaced the line with
transform.rotation = new Quaternion(0, 0, angle, 1);
since rotation takes a quaternion but still no luck, the character doesn't rotate with the mouse, but this has led me to believe the problem might be that the position of the mouse isnt being picked up properly or another component is stopping it from working somehow.
doing some debugging it turns out that anglerad is co$$anonymous$$g out as 0 and therefore angle is co$$anonymous$$g out 0, checking the mouse position came up with correct numbers so there is something wrong with this line
float AngleRad = $$anonymous$$athf.Atan2 (mousePos.y - transform.position.y, mousePos.x - transform.position.x);
I can't tell you the problem, but maybe you could use the code provided in the API example: https://docs.unity3d.com/ScriptReference/$$anonymous$$athf.Atan2.html
You translate the mouse position into local coordinates and then calculate the angle. I don't know if it makes any difference, but you can try.
In the end I found another answer and copied its solution, here is what I got.
Vector3 dir = Input.mousePosition - cam.WorldToScreenPoint (transform.position);
float angle = $$anonymous$$athf.Atan2 (dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Rotate object with mouse 1 Answer
How to rotate Character on Y axis along with the mouse rotation? 0 Answers
Player mouse rotation 2 Answers
2d game facing bullets to mouse 2 Answers