Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to make a Skiing Game - Help needed...

Discussion in 'Getting Started' started by ugal, Nov 3, 2023.

  1. ugal

    ugal

    Joined:
    Feb 15, 2020
    Posts:
    7
    Hello every body, I am a newbie trying to make a ski game
    Started this project a few years back during the quarantine (had time to spend lol)

    I am now trying to make my input with the input system and use a controller but I am having issues

    The AddRelativeTorque is not working as I understand it should...

    I'll share my code and a video to illustrate my problem below

    I have floats speedFlip and speedTurn that should impact the speed my player turns on the ground and spins in the air
    For this I add a relative torque to my player Vector.up which is multiplied by the input value of my joystick and the 2 float described above, but even though this value manages to be modified as I would like, the angular velocity of my player is the same no matter what the joystick input is

    On the screen you can see the values of speedFlip and speedTurn (which should impact the value of the torque added to the player) and the angular velocity of the player

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6. using UnityEngine.UI;
    7. using static System.Net.Mime.MediaTypeNames;
    8.  
    9.  
    10. public class PlayerController : MonoBehaviour
    11. {
    12.     public Rigidbody rb;
    13.  
    14.     public bool isGrounded;
    15.  
    16.  
    17.     float boostForce = 200;
    18.     float jumpForce = 300;
    19.     float speedTurn = 1000; //multiplier that affect the speed with which the player turns
    20.     float speedTurnReduc = 0.5f; //amount of reduction applied to the speedTurn, while crouching before jumping
    21.     float boostDragReduc = 0; //reduce drag while crouching
    22.     float brakeForce = 180;
    23.     float speedFlip = 1000; //multiplier that affect the speed with which the player flips and spins in the air
    24.  
    25.  
    26.     float speedTurnInitial;
    27.     float initialDrag;
    28.  
    29.     Vector3 startingPosition = Vector3.zero;
    30.  
    31.     float speedCount;
    32.     int speedDisplay;
    33.     public UnityEngine.UI.Text speedText;
    34.  
    35.  
    36.     public UnityEngine.UI.Text dragText;
    37.     public UnityEngine.UI.Text isGroundedText;
    38.     public UnityEngine.UI.Text angVelText;
    39.  
    40.     public UnityEngine.UI.Text speedTurnText;
    41.     public UnityEngine.UI.Text speedSpinText;
    42.  
    43.  
    44.     //------------------------------------------- unity input system -------------------------------------------
    45.     PlayerControls controls;
    46.  
    47.     float turn;
    48.     float brake;
    49.     float spin;
    50.  
    51.     private void Awake()
    52.     {
    53.         controls = new PlayerControls();
    54.  
    55.         controls.Gameplay.Boost.performed += ctx => Boost();
    56.  
    57.         controls.AllTime.Restart.performed += ctx => Restart();
    58.  
    59.         controls.Gameplay.Turn.performed += ctx => turn = ctx.ReadValue<float>();
    60.         controls.Gameplay.Turn.canceled += ctx => turn = 0;
    61.  
    62.         controls.Gameplay.Jump.performed += ctx => Crouch();
    63.         controls.Gameplay.Jump.canceled += ctx => Jump();
    64.  
    65.         controls.Gameplay.Brake.performed += ctx => Brake();
    66.         controls.Gameplay.Brake.performed += ctx => brake = ctx.ReadValue<float>();
    67.         controls.Gameplay.Brake.canceled += ctx => Unbrake();
    68.  
    69.         controls.Air.Spin.performed += ctx => spin = ctx.ReadValue<float>();
    70.         controls.Air.Spin.canceled += ctx => spin = 0;
    71.  
    72.         controls.Air.Test.performed += ctx => Test();
    73.     }
    74.  
    75.     void Test()
    76.     {
    77.         Debug.Log("Active Action Map = AIR");
    78.     }
    79.  
    80.     void Boost()
    81.     {
    82.         if (isGrounded)
    83.         {
    84.             rb.AddRelativeForce(Vector3.forward * boostForce, ForceMode.Impulse);
    85.             Debug.Log("BOOST!!");
    86.         }
    87.     }
    88.  
    89.     void Restart()
    90.     {
    91.         transform.SetPositionAndRotation(Vector3.up, Quaternion.Euler(0.0f, 0.0f, 0.0f));
    92.         rb.velocity = Vector3.zero;
    93.         rb.angularVelocity = Vector3.zero;
    94.     }
    95.  
    96.     void Crouch()
    97.     {
    98.         if (isGrounded)
    99.         {
    100.             rb.drag = boostDragReduc * initialDrag; // reduce drag force while pressing "space"
    101.             speedTurn *= speedTurnReduc;            // reduc speed turn while pressing "space"
    102.             Debug.Log("CROUCH!!");
    103.         }
    104.     }
    105.     void Jump()
    106.     {
    107.         if (isGrounded)
    108.         {
    109.             rb.AddRelativeForce(Vector3.up * jumpForce, ForceMode.Impulse);
    110.             Debug.Log("JUMP!!");
    111.         }
    112.     }
    113.  
    114.     void Brake()
    115.     {
    116.         if (isGrounded)
    117.         {
    118.             rb.drag = 0.1f * brakeForce * brake * initialDrag;
    119.             Debug.Log("BRAKE!!");
    120.         }
    121.     }
    122.     void Unbrake()
    123.     {
    124.         if (isGrounded)
    125.         {
    126.             rb.drag = initialDrag;
    127.             Debug.Log("UNBRAKE!!");
    128.         }
    129.     }
    130.  
    131.  
    132.     private void OnEnable()
    133.     {
    134.         controls.Gameplay.Enable();
    135.         controls.AllTime.Enable();
    136.     }
    137.  
    138.     private void OnDisable()
    139.     {
    140.         controls.Gameplay.Disable();
    141.         controls.AllTime.Disable();
    142.     }
    143.     //------------------------------------------- unity input system -------------------------------------------
    144.  
    145.  
    146.  
    147.  
    148.  
    149.  
    150.  
    151.  
    152.     // Use this for initialization
    153.     void Start()
    154.     {
    155.         rb = gameObject.GetComponent<Rigidbody>();
    156.  
    157.         //SPEEDTEXT
    158.         speedText.text = speedDisplay.ToString();
    159.  
    160.         //DRAGTEXT
    161.         dragText.text = "drag : " + rb.drag.ToString();
    162.  
    163.  
    164.         //ANGULAR DRAG TEXT
    165.         angVelText.text = "angular velocity : " + rb.angularVelocity.ToString();
    166.  
    167.         //hide text isGrounded
    168.         isGroundedText.gameObject.SetActive(false);
    169.  
    170.         initialDrag = rb.drag;
    171.         speedTurnInitial = speedTurn;
    172.  
    173.     }
    174.  
    175.  
    176.     // these two functions allow me to know when the player is in contact with the ground or a rail
    177.     // with the two bool isGrounded
    178.  
    179.     void OnCollisionEnter(Collision collision)
    180.     {
    181.         if (collision.gameObject.layer == 10 && !isGrounded)
    182.         {
    183.             isGrounded = true;
    184.             controls.Air.Disable();
    185.             controls.Gameplay.Enable();
    186.             rb.angularVelocity = Vector3.zero;// set angular veloctiy to zero (as soon as he hits the ground)
    187.  
    188.         }
    189.     }
    190.  
    191.     void OnCollisionExit(Collision collision)
    192.     {
    193.         if (collision.gameObject.layer == 10 && isGrounded)
    194.         {
    195.             isGrounded = false;
    196.             rb.drag = initialDrag;  // reset the drag force when leaving the ground
    197.             speedTurn = speedTurnInitial; // reset the speed turn when leaving the ground
    198.             controls.Air.Enable();
    199.             controls.Gameplay.Disable();
    200.             rb.angularVelocity = Vector3.zero; //cette ligne permet de ne pas continuer à tourner en sautant, sans cette ligne le joueur continue de tourner lorsque il saute, en consequence de la force ajouté pour le faire tourner AU SOL
    201.  
    202.         }
    203.     }
    204.  
    205.  
    206.  
    207.  
    208.  
    209.  
    210.     private void Update()
    211.     {
    212.  
    213.  
    214.  
    215.         ////----------------------------------------------------- the player is ON THE GROUND ------------------------------------------------
    216.         if (isGrounded)
    217.         {
    218.  
    219.             BlockXSlide();
    220.  
    221.             isGroundedText.gameObject.SetActive(true); //show text "isGrounded"
    222.  
    223.         }
    224.         else //the player is NOT on the ground
    225.         {
    226.             isGroundedText.gameObject.SetActive(false); //hide "isGrounded" Text
    227.         }
    228.     }
    229.  
    230.  
    231.     void FixedUpdate()
    232.     {
    233.         //unity input system - to spin
    234.         float s = spin * speedFlip * Time.fixedDeltaTime;
    235.         rb.AddRelativeTorque(s * Vector3.up, ForceMode.VelocityChange);
    236.  
    237.         //unity input system - to turn
    238.         float t = turn * speedTurn * Time.fixedDeltaTime;
    239.         rb.AddRelativeTorque(t * Vector3.up, ForceMode.VelocityChange);
    240.  
    241.         //SPEEDTURNTEXT
    242.         speedTurnText.text = "speedTurn : " + t.ToString();
    243.  
    244.         //SPEEDSPIN TEXT
    245.         speedSpinText.text = "speedSpin : " + s.ToString();
    246.  
    247.  
    248.  
    249.         //SPEEDTEXT
    250.         float movementPerFrame = Vector3.Distance(startingPosition, transform.position);            //it is changing and updating it self too quickly , you don't really have time to see your speed,
    251.         speedCount = movementPerFrame / Time.deltaTime;                                             // it should stay at least 1 sec for each number (or just update the number every second seems mor correct)
    252.         startingPosition = transform.position;
    253.         speedDisplay = (int)speedCount;
    254.         speedText.text = speedDisplay.ToString();
    255.  
    256.         //DRAGTEXT
    257.         dragText.text = "drag : " + rb.drag.ToString();
    258.         angVelText.text = "angular velocity : " + rb.angularVelocity.ToString();
    259.     }
    260.  
    261.  
    262.     //blocks sliding in the X axis of the player
    263.     void BlockXSlide()
    264.     {
    265.         //Prevent sideways sliding of rigidbody. Set this factor between 0 and 1.
    266.         float LateralSpeedFactor = 0.1f;
    267.  
    268.         //Inverse transform rigidbody velocity from world to local coordinates
    269.         Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
    270.  
    271.         //Remove X (sideways) component of local velocity
    272.         localVelocity.x *= LateralSpeedFactor;
    273.  
    274.         //Apply new velocity to rigidbody by transforming local velocity back to world coordinates
    275.         rb.velocity = transform.TransformDirection(localVelocity);
    276.  
    277.     }
    278. }
     
  2. ugal

    ugal

    Joined:
    Feb 15, 2020
    Posts:
    7
    Just for info, I get the value of the joystick in the Awake() and use that value a bit further down in the code in the FixedUpdate()