Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Rotation Issue for Player to mouse cursor

Discussion in '2D' started by Grengoshi, Apr 19, 2020.

  1. Grengoshi

    Grengoshi

    Joined:
    Apr 11, 2020
    Posts:
    7
    I have never posted on here before, I try to make sure if this is the right place. If it is not I apologize ahead of time.
    In regards to my issue: I have mad a PlayerController script and in that script is a LookAtMouse Method. The problem is I can get the player to rotate to the mouse but it only works if i do 2 or 3 rounds around the character for the character to move its rotation. I have been working on this for a while and I am about to quit. I am a little better than a beginner.
    Also I do not want to use Rigidbody for any form of movement or controls, and any tutorial I have read says this is fine, except the late update. that was me trying to do something different.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 3.0f;
    9.  
    10.     public Camera cam;
    11.  
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         Movement();
    21.      
    22.     }
    23.     private void FixedUpdate()
    24.     {
    25.         RotateToMouse();
    26.     }
    27.     private void Movement()
    28.     {
    29.  
    30.         float moveX = Input.GetAxisRaw("Horizontal");
    31.  
    32.         float moveY = Input.GetAxis("Vertical");
    33.  
    34.         Vector2 movementXY = new Vector2(moveX, moveY).normalized;
    35.  
    36.         transform.Translate(movementXY * speed * Time.deltaTime);
    37.  
    38.     }
    39.  
    40.     private void RotateToMouse()
    41.     {
    42.  
    43.         Vector2 mousePos = Input.mousePosition;
    44.         Vector2 mousePosition = cam.WorldToScreenPoint(new Vector2(mousePos.x, mousePos.y));
    45.  
    46.         // Debug.Log(mousePosition);
    47.  
    48.         Vector2 playerPosition = new Vector2(transform.position.x, transform.position.y);
    49.  
    50.         Vector2 directionVector = new Vector2(mousePosition.x - playerPosition.x, mousePosition.y - playerPosition.y) * Mathf.Rad2Deg;
    51.  
    52.         float aimAngle = Mathf.Atan2(directionVector.y, directionVector.x) * Mathf.Rad2Deg -90;
    53.         Quaternion aimRotation = Quaternion.Euler(new Vector3(0, 0, aimAngle));
    54.  
    55.         transform.rotation = aimRotation;
    56.  
    57.     }
    58. }
    59.  
    but I am not a coder in the slightest thought. Help Please and Thank you
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Seems your problem is you're mixing up your screen point and world point. Input.mouseposition is already a screen point, and transform.position is a world point. However, your code treats them exactly opposite to this. Try changing cam.WorldToScreenPoint to cam.ScreenToWorldPoint.
     
  3. Grengoshi

    Grengoshi

    Joined:
    Apr 11, 2020
    Posts:
    7
    Thank you!

    such a "simple" thing and i been wrapping my head around what I did wrong!

    That fixed it! TY!