Search Unity

How do i change one variable relative to another one?

Discussion in 'Scripting' started by djc51401, Jun 12, 2020.

  1. djc51401

    djc51401

    Joined:
    Apr 3, 2020
    Posts:
    12
    so im working on a racing game, and im working on basic driving movement. I made it a rigidbody controller and heres the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public float maxTurnRadius;
    9.     public float carSpeed;
    10.     public float maxSpeed;
    11.  
    12.     [SerializeField] float turnRadius;
    13.  
    14.     float speed;
    15.  
    16.     Rigidbody rb;
    17.  
    18.     Vector3 lookDirection;
    19.  
    20.     bool isAccelerating;
    21.     bool isBraking;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         rb = GetComponent<Rigidbody>();
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.  
    32.         speed = carSpeed * 100;
    33.  
    34.         if (isAccelerating){
    35.             rb.AddForce(transform.forward * speed * Time.deltaTime);
    36.             if(turnRadius >= maxTurnRadius)
    37.             {
    38.                 turnRadius = maxTurnRadius;
    39.             }
    40.         }
    41.  
    42.         if (isBraking)
    43.         {
    44.             rb.AddForce(-transform.forward * speed/2f * Time.deltaTime);
    45.         }
    46.  
    47.         if(rb.velocity.z >= maxSpeed)
    48.         {
    49.             rb.velocity = new Vector3(0,0,maxSpeed);
    50.         }
    51.         if (rb.velocity.z > 0.5f)
    52.         {
    53.             transform.rotation *= Quaternion.Euler(lookDirection * Time.deltaTime);
    54.         }
    55.     }
    56.  
    57.     // Update is called once per frame
    58.  
    59.     public void onAccelerate(InputAction.CallbackContext context)
    60.     {
    61.         isAccelerating = context.performed;
    62.     }
    63.  
    64.     public void onBrake(InputAction.CallbackContext context)
    65.     {
    66.         isBraking = context.performed;
    67.     }
    68.  
    69.     public void onTurn(InputAction.CallbackContext context)
    70.     {
    71.         float x = context.ReadValue<float>();
    72.         lookDirection = new Vector3(0, x * turnRadius, 0);
    73.     }
    74. }
    75.  
    this works good and all, but i want the turn radius number to be relative to the velocity on the rigidbody. so when the velocity is 0, the turn radius is 0, and when the velocity is at maxSpeed, i want the turn radius to be at maxTurnRadius. With how it is now, the car will turn no matter what speed its moving (Which obviously looks off).

    Any suggestions are appreciated!
     
  2. Hint: (use InverseLerp on the velocity and maxspeed relation) and then multiply the turnradius with it.

    (Edited to be more clear)
     
    djc51401 likes this.
  3. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    you can use this formula
    Code (CSharp):
    1. turnRadius = MaxTurnRadius * ( velocity/maxvelocity)
     
  4. djc51401

    djc51401

    Joined:
    Apr 3, 2020
    Posts:
    12
    so i was able to make it work, but now i have another problem. when im moving forward and i turn while the turn radius is low, itll keep turning at that rate even after the turn radius goes up. i have to let go of the turn button/stick, and then push it again to go to the new radius.
     
  5. That's because your input system setup, I guess. You will need to subscribe to the action's performed event (so you can perform changes in every update and not just when a button was pressed/released). But you should check the documentation to be sure.
     
  6. IbiTheDon

    IbiTheDon

    Joined:
    Jul 26, 2023
    Posts:
    6
    Here is my issue in my double jumping script, Line 10 is the issue:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. [RequireComponent(typeof(Rigidbody))]
    5.  
    6. public class Player_controller : MonoBehaviour
    7. {
    8.     [SerializeField] private InputActionReference Jump_actionReference;
    9.     [SerializeField] private float jumpForce = 500.0f;  // Jump force for initial jump
    10.     public float doubleJumpForce = jumpForce * 0.75;      // Jump force for double jump
    11.     public int maxJumps = 2;                  // Maximum number of jumps (including initial jump)
    12.  
    13.     private Rigidbody rb;
    14.     private int jumpsRemaining;
    15.  
    16.     private void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.         jumpsRemaining = maxJumps;
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         // Check for jump trigger input (e.g., button press)
    25.         if (Input.GetButtonDown("Jump") && jumpsRemaining > 0)
    26.         {
    27.             Jump();
    28.         }
    29.     }
    30.  
    31.     private void Jump()
    32.     {
    33.         if (jumpsRemaining == maxJumps)
    34.         {
    35.             rb.velocity = new Vector3(rb.velocity.x, 0.0f, rb.velocity.z);  // Clear vertical velocity before initial jump
    36.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    37.         }
    38.         else
    39.         {
    40.             rb.velocity = new Vector3(rb.velocity.x, 0.0f, rb.velocity.z);  // Clear vertical velocity before double jump
    41.             rb.AddForce(Vector3.up * doubleJumpForce, ForceMode.Impulse);
    42.         }
    43.  
    44.         jumpsRemaining--;
    45.     }
    46.  
    47.     private void OnCollisionEnter(Collision collision)
    48.     {
    49.         // Reset jump state when landing on the ground
    50.         if (collision.gameObject.CompareTag("Ground"))
    51.         {
    52.             jumpsRemaining = maxJumps;
    53.         }
    54.     }
    55. }
    I want the doubleJumpForce value to change in accordance to the jumpForce value but it shows this error:
    'A field Initializer cannot reference the non-static field, method, or property Player_Controller.jumpForce'
    Note: Player_Controller is my .cs file name.
     
  7. IbiTheDon

    IbiTheDon

    Joined:
    Jul 26, 2023
    Posts:
    6
    Chat Gpt just fixed the code for me XD