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

Question Player does not face the center of the screen

Discussion in '2D' started by artmagic, Apr 11, 2023.

  1. artmagic

    artmagic

    Joined:
    Apr 11, 2023
    Posts:
    3
    I am creating a 2D game. With the following code, I am making the player move in a circular motion. I want the player to always face the center of the screen, but I don't know where to make modifications.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMove : MonoBehaviour
    6. {
    7. public Transform center; // Object to be the center of rotation
    8. public float rotationSpeed = 50f; // Rotation speed
    9. private Rigidbody2D rb; // Reference to the Rigidbody2D component
    10. void Start()
    11. {
    12.     // Set the initial position
    13.     transform.position = new Vector3(0, -2, 0);
    14.     // Get the Rigidbody2D component
    15.     rb = GetComponent<Rigidbody2D>();
    16. }
    17.  
    18. void Update()
    19. {
    20.     // Calculate the direction vector towards the rotation center
    21.     Vector3 direction = center.position - transform.position;
    22.  
    23.     // Calculate the quaternion representing the direction vector towards the rotation center
    24.     Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, direction);
    25.  
    26.     // Rotate the player towards the rotation center
    27.     transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
    28.  
    29.     // If the left arrow key or A key is pressed
    30.     if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
    31.     {
    32.         // Rotate the player around the center with the rotation speed
    33.         transform.RotateAround(center.position, Vector3.forward, rotationSpeed * Time.deltaTime);
    34.         // Update the angular velocity of the Rigidbody2D component
    35.         rb.angularVelocity = rotationSpeed;
    36.     }
    37.     // If the right arrow key or D key is pressed
    38.     else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
    39.     {
    40.         // Rotate the player around the center with the opposite rotation speed
    41.         transform.RotateAround(center.position, Vector3.forward, -rotationSpeed * Time.deltaTime);
    42.         // Update the angular velocity of the Rigidbody2D component
    43.         rb.angularVelocity = -rotationSpeed;
    44.     }
    45.     // If neither the left arrow key nor the right arrow key is pressed
    46.     else
    47.     {
    48.         // Set the angular velocity of the Rigidbody2D component to 0
    49.         rb.angularVelocity = 0f;
    50.     }
    51. }
    52. }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    What is happening now? What direction is the player facing? Does it rotate at all? Need a few more details
     
    artmagic likes this.
  3. artmagic

    artmagic

    Joined:
    Apr 11, 2023
    Posts:
    3
    It is facing in a strange direction. Regardless of the position of x and y, it is facing around -60 degrees in the z direction.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    That doesn't really add much information. One thing that can be said is that you should never, ever modify the Transform when using 2D physics. Movement and Transform changes are done by a Rigidbody2D and you should always uses is API.

    I don't understand why you're setting the Transform (you shouldn't do this) but also then changing the angular velocity of the Rigidbody2D so it rotates. It cannot rotate, you keep stomping over the Transform rotation.

    No idea if this is a Dynamic or Kinematic body but for explicit positioning you should always use MovePosition and MoveRotation.

    Do you know that physics doesn't run per-frame by default? You're changing physics stuff per-frame.

    You should upload an image/video and show the configuration.
     
    artmagic likes this.
  5. artmagic

    artmagic

    Joined:
    Apr 11, 2023
    Posts:
    3
    Thank you both very much. I was finally able to understand the cause.
    It was probably using RigidBody2D and Transform / Rotation together.

    If I post any other questions in the future, I intend to attach images or videos as much as possible.
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Glad you got it working, good luck.
     
    artmagic likes this.