Search Unity

Question How to make the players turning arc circular in a 2d racing game

Discussion in 'Physics' started by WraithLightGames, Jan 15, 2021.

  1. WraithLightGames

    WraithLightGames

    Joined:
    Aug 19, 2020
    Posts:
    8
    For context I am working on a 2D racing game and am trying to set up the player movement. The way that I am doing this is by adding forces to the player via the Rigidbody 2D component. The problem I have run into is that the turning arc is not circular like i want it to be. Instead it takes a teardrop shape which makes the movement feel out of control and unpredictable when steering. (I have a screenshot below to show what the turning arc looks like). I have absolutely no idea how to fix this and any help would be greatly appreciated.

    Some info you should know about my player controller script:

    I wrote a FSM for the player using this tutorial. The way the FSM works is: a script called Player Controller is attached to the player object. This script handles the states and calls the behavior methods of the state that is currently active. (For example: the Update method in the current state is called by the Update method in the player controller every frame).







    Turning Arc:
    Turning arc.png

    Player controller script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     #region StateVariables
    8.     private PlayerBaseState CurrentState;
    9.  
    10.     public readonly PlayerDriveState DriveState = new PlayerDriveState();
    11.     public readonly PlayerIdleState IdleState = new PlayerIdleState();
    12.     public readonly PlayerStunnedState StunnedState = new PlayerStunnedState();
    13.     #endregion
    14.  
    15.     public LapManager lapManager;
    16.  
    17.     #region PhysicsVariables
    18.  
    19.     public Rigidbody2D rb2;
    20.  
    21.  
    22.  
    23.  
    24.     #endregion
    25.  
    26.     #region DriveStateVariables
    27.     public float TerminalVelocity;
    28.     public float RocketPower;
    29.  
    30.     public float BrakePower;
    31.  
    32.     public float TurnSpeed;
    33.     #endregion
    34.  
    35.     public void Awake()
    36.     {
    37.         //Stores a reference to the LapManager script attached to the GameManager object
    38.         lapManager = GameObject.FindGameObjectWithTag("Manager").GetComponent<LapManager>();
    39.  
    40.         //Calls the IdleState
    41.         TransitionToState(IdleState);
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     void Update()
    46.     {
    47.         CurrentState.Update(this);
    48.     }
    49.  
    50.     public void FixedUpdate()
    51.     {
    52.         CurrentState.FixedUpdate(this);
    53.     }
    54.  
    55.     public void OnTriggerEnter2D(Collider2D collision)
    56.     {
    57.         CurrentState.OnTriggerEnter(this, collision);
    58.     }
    59.  
    60.     public void OnCollisionEnter2D(Collision2D collision)
    61.     {
    62.         CurrentState.OnCollisionEnter(this, collision);
    63.     }
    64.  
    65.     public void TransitionToState(PlayerBaseState state)
    66.     {
    67.         CurrentState = state;
    68.         CurrentState.EnterState(this);
    69.     }
    70. }
    71.  
    Player Drive State:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerDriveState : PlayerBaseState
    6. {
    7.     private Rigidbody2D prb2;
    8.     private Transform pt;
    9.  
    10.     private float TerminalVelocity;
    11.     private float RocketPower;
    12.  
    13.     private Vector2 ForceToAdd;
    14.     private Vector3 RotSpeed;
    15.  
    16.     //Gets called when the State is activated
    17.     public override void EnterState(PlayerController player)
    18.     {
    19.         //Stores references to the Player's components
    20.         prb2 = player.rb2;
    21.         pt = player.transform;
    22.  
    23.        
    24.         TerminalVelocity = player.TerminalVelocity;
    25.         RocketPower = player.RocketPower;
    26.  
    27.         //Sets up a turning vector for the player to use
    28.         RotSpeed = new Vector3(0, 0, player.TurnSpeed);
    29.     }
    30.  
    31.     //Gets called every frame
    32.     public override void FixedUpdate(PlayerController player)
    33.     {
    34.        
    35.         //Thrust
    36.         if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
    37.         {
    38.             ForceToAdd = new Vector2(RocketPower * pt.up.x, RocketPower * pt.up.y);
    39.  
    40.             prb2.AddForce(ForceToAdd * Time.fixedDeltaTime, ForceMode2D.Impulse);
    41.         }
    42.         else if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
    43.         {
    44.             ForceToAdd = new Vector2(RocketPower * -pt.up.x, RocketPower * -pt.up.y);
    45.  
    46.             prb2.AddForce(ForceToAdd * Time.fixedDeltaTime, ForceMode2D.Impulse);
    47.         }
    48.  
    49.         prb2.velocity = Vector2.ClampMagnitude(prb2.velocity, TerminalVelocity);
    50.     }
    51.    
    52.     //Gets called every frame
    53.     public override void Update(PlayerController player)
    54.     {
    55.         //Steering
    56.         float i = Input.GetAxisRaw("Horizontal");
    57.  
    58.        
    59.         pt.Rotate(-RotSpeed * i * Time.fixedDeltaTime);
    60.  
    61.        
    62.     }
    63.  
    64.  
    65.     //Gets called whenever the player collides with something
    66.     public override void OnCollisionEnter(PlayerController player, Collision2D col)
    67.     {
    68.  
    69.     }
    70.  
    71.     //Gets called when the player collides with a trigger
    72.     public override void OnTriggerEnter(PlayerController player, Collider2D col)
    73.     {
    74.  
    75.     }
    76.  
    77.    
    78. }
    79.