1

I'm building a topdown game with my main player rotating towards the mouse pointer but for some reason the player looks at the pointer from his right(his x axis) and i need need him to look from his Y.

I tried multiple ways and still the same as in i tried changing the vector from vector3 to vector2 but it will make things i don't need it to do, and i even tried using Quaternions.

void controlScheme()
{
    if (Input.GetKey(KeyCode.W))
    {
        transform.Translate(Vector3.up * PlayerSpeed * Time.deltaTime,Space.World);
    }
    if (Input.GetKey(KeyCode.S))
    {
        transform.Translate(Vector3.down * PlayerSpeed * Time.deltaTime,Space.World);
    }
    if (Input.GetKey(KeyCode.A))
    {
        transform.Translate(Vector3.left * PlayerSpeed * Time.deltaTime,Space.World);
    }
    if (Input.GetKey(KeyCode.D))
    {
        transform.Translate(Vector3.right * PlayerSpeed * Time.deltaTime,Space.World);
    }

    transform.up = dir;*/

    var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
    var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

the only weird thing that there is no code the tell the engine to make the player rotate towards the mouse from the player's right side.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
WillYum
  • 61
  • 1
  • 8
  • By any chance this post solves it? Seems like similar problem. [https://stackoverflow.com/questions/36615476/unity-gameobject-look-at-mouse](https://stackoverflow.com/questions/36615476/unity-gameobject-look-at-mouse) –  Mar 25 '19 at 07:50
  • Could you post an image of your game / character ? – Jichael Mar 25 '19 at 08:53
  • A small video about how the player rotated towards the mouse. Know that the player has a shape of an arrow it was for other purposes, so don't think i'm creating a pointer but that's the player for now https://imgur.com/a/XxbFOVz – WillYum Mar 25 '19 at 15:04
  • The link you provided me with seems to be focused to isometric games while i'm trying for Topdown 2d game. The code that is provided is partially working the only problem is that the player is rotating in a circular way not only X and Y. i must edit it in some way. – WillYum Mar 25 '19 at 15:34
  • What vector direction is the tip of the arrow pointing when there is no rotation? – Ruzihm Mar 25 '19 at 16:40
  • the top of the pointer is towards Y – WillYum Mar 25 '19 at 18:06

2 Answers2

0

One solution is to find a vector you want the character to rotate "from" -- the direction of the sprite's "front" -- and where it should rotate "to" -- the direction from the character to the mouse position -- and then use transform.rotation.SetFromToRotation to set any rotation necessary to make that change:

Vector3 desiredDirection = Camera.main.WorldToScreenPoint(transform.position) - Input.mousePosition;
Vector3 startDirection = Vector3.up; // the vector direction of the character's
                                     // "front" before any rotation is applied.

transform.rotation.SetFromToRotation(startDirection, desiredDirection);
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
0
 Vector2 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - this.transform.position;


    float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
    this.transform.rotation = Quaternion.Euler(0f, 0f, rot_z -90);

i found a way with code though still the character was looking at the mouse pointer from his right, i rotated him -90 degrees so it can look better again, my problem was a bit dumb but still there wasn't a proper way to fix this. Thank you guys. :D

WillYum
  • 61
  • 1
  • 8