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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using static System.Net.Mime.MediaTypeNames;
public class PlayerController : MonoBehaviour
{
public Rigidbody rb;
public bool isGrounded;
float boostForce = 200;
float jumpForce = 300;
float speedTurn = 1000; //multiplier that affect the speed with which the player turns
float speedTurnReduc = 0.5f; //amount of reduction applied to the speedTurn, while crouching before jumping
float boostDragReduc = 0; //reduce drag while crouching
float brakeForce = 180;
float speedFlip = 1000; //multiplier that affect the speed with which the player flips and spins in the air
float speedTurnInitial;
float initialDrag;
Vector3 startingPosition = Vector3.zero;
float speedCount;
int speedDisplay;
public UnityEngine.UI.Text speedText;
public UnityEngine.UI.Text dragText;
public UnityEngine.UI.Text isGroundedText;
public UnityEngine.UI.Text angVelText;
public UnityEngine.UI.Text speedTurnText;
public UnityEngine.UI.Text speedSpinText;
//------------------------------------------- unity input system -------------------------------------------
PlayerControls controls;
float turn;
float brake;
float spin;
private void Awake()
{
controls = new PlayerControls();
controls.Gameplay.Boost.performed += ctx => Boost();
controls.AllTime.Restart.performed += ctx => Restart();
controls.Gameplay.Turn.performed += ctx => turn = ctx.ReadValue<float>();
controls.Gameplay.Turn.canceled += ctx => turn = 0;
controls.Gameplay.Jump.performed += ctx => Crouch();
controls.Gameplay.Jump.canceled += ctx => Jump();
controls.Gameplay.Brake.performed += ctx => Brake();
controls.Gameplay.Brake.performed += ctx => brake = ctx.ReadValue<float>();
controls.Gameplay.Brake.canceled += ctx => Unbrake();
controls.Air.Spin.performed += ctx => spin = ctx.ReadValue<float>();
controls.Air.Spin.canceled += ctx => spin = 0;
controls.Air.Test.performed += ctx => Test();
}
void Test()
{
Debug.Log("Active Action Map = AIR");
}
void Boost()
{
if (isGrounded)
{
rb.AddRelativeForce(Vector3.forward * boostForce, ForceMode.Impulse);
Debug.Log("BOOST!!");
}
}
void Restart()
{
transform.SetPositionAndRotation(Vector3.up, Quaternion.Euler(0.0f, 0.0f, 0.0f));
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
void Crouch()
{
if (isGrounded)
{
rb.drag = boostDragReduc * initialDrag; // reduce drag force while pressing "space"
speedTurn *= speedTurnReduc; // reduc speed turn while pressing "space"
Debug.Log("CROUCH!!");
}
}
void Jump()
{
if (isGrounded)
{
rb.AddRelativeForce(Vector3.up * jumpForce, ForceMode.Impulse);
Debug.Log("JUMP!!");
}
}
void Brake()
{
if (isGrounded)
{
rb.drag = 0.1f * brakeForce * brake * initialDrag;
Debug.Log("BRAKE!!");
}
}
void Unbrake()
{
if (isGrounded)
{
rb.drag = initialDrag;
Debug.Log("UNBRAKE!!");
}
}
private void OnEnable()
{
controls.Gameplay.Enable();
controls.AllTime.Enable();
}
private void OnDisable()
{
controls.Gameplay.Disable();
controls.AllTime.Disable();
}
//------------------------------------------- unity input system -------------------------------------------
// Use this for initialization
void Start()
{
rb = gameObject.GetComponent<Rigidbody>();
//SPEEDTEXT
speedText.text = speedDisplay.ToString();
//DRAGTEXT
dragText.text = "drag : " + rb.drag.ToString();
//ANGULAR DRAG TEXT
angVelText.text = "angular velocity : " + rb.angularVelocity.ToString();
//hide text isGrounded
isGroundedText.gameObject.SetActive(false);
initialDrag = rb.drag;
speedTurnInitial = speedTurn;
}
// these two functions allow me to know when the player is in contact with the ground or a rail
// with the two bool isGrounded
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.layer == 10 && !isGrounded)
{
isGrounded = true;
controls.Air.Disable();
controls.Gameplay.Enable();
rb.angularVelocity = Vector3.zero;// set angular veloctiy to zero (as soon as he hits the ground)
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.layer == 10 && isGrounded)
{
isGrounded = false;
rb.drag = initialDrag; // reset the drag force when leaving the ground
speedTurn = speedTurnInitial; // reset the speed turn when leaving the ground
controls.Air.Enable();
controls.Gameplay.Disable();
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
}
}
private void Update()
{
////----------------------------------------------------- the player is ON THE GROUND ------------------------------------------------
if (isGrounded)
{
BlockXSlide();
isGroundedText.gameObject.SetActive(true); //show text "isGrounded"
}
else //the player is NOT on the ground
{
isGroundedText.gameObject.SetActive(false); //hide "isGrounded" Text
}
}
void FixedUpdate()
{
//unity input system - to spin
float s = spin * speedFlip * Time.fixedDeltaTime;
rb.AddRelativeTorque(s * Vector3.up, ForceMode.VelocityChange);
//unity input system - to turn
float t = turn * speedTurn * Time.fixedDeltaTime;
rb.AddRelativeTorque(t * Vector3.up, ForceMode.VelocityChange);
//SPEEDTURNTEXT
speedTurnText.text = "speedTurn : " + t.ToString();
//SPEEDSPIN TEXT
speedSpinText.text = "speedSpin : " + s.ToString();
//SPEEDTEXT
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,
speedCount = movementPerFrame / Time.deltaTime; // it should stay at least 1 sec for each number (or just update the number every second seems mor correct)
startingPosition = transform.position;
speedDisplay = (int)speedCount;
speedText.text = speedDisplay.ToString();
//DRAGTEXT
dragText.text = "drag : " + rb.drag.ToString();
angVelText.text = "angular velocity : " + rb.angularVelocity.ToString();
}
//blocks sliding in the X axis of the player
void BlockXSlide()
{
//Prevent sideways sliding of rigidbody. Set this factor between 0 and 1.
float LateralSpeedFactor = 0.1f;
//Inverse transform rigidbody velocity from world to local coordinates
Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
//Remove X (sideways) component of local velocity
localVelocity.x *= LateralSpeedFactor;
//Apply new velocity to rigidbody by transforming local velocity back to world coordinates
rb.velocity = transform.TransformDirection(localVelocity);
}
}