Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Change lane of vehicle when hold left mouse

Discussion in 'Scripting' started by Bonchanx, Apr 3, 2024.

  1. Bonchanx

    Bonchanx

    Joined:
    Mar 11, 2023
    Posts:
    4
    Sorry for my bad english but i have some question.
    I'm making a racing game with 2 lanes. Now I want the car to move forward in a straight line and when I hold down the left mouse button, the car will start moving gradually to the left (second lane). 2) and when the object moves to the middle of the 2nd lane, it will move in a straight line. When you release the left mouse button, the car will gradually move back to the first lane. I tried doing this but it doesn't work.
    Code (CSharp):
    1. using UnityEngine;
    2. public class Move : MonoBehaviour
    3. {
    4.     private Rigidbody rb;
    5.     public float speed;
    6.     private Vector3 dir;
    7.     private bool goLeft;
    8.     private bool goRight;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         rb = GetComponent<Rigidbody>();
    13.         dir = Vector3.forward;
    14.     }
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if (Input.GetMouseButtonDown(0))
    19.         {
    20.             dir = new Vector3(-1f, 0, 1f);
    21.             return;
    22.         }
    23.         if (Input.GetMouseButtonUp(0))
    24.         {
    25.             dir = new Vector3(1f, 0, 1f);
    26.             return;
    27.         }
    28.         if (transform.position.x > -4f && transform.position.x < -2f)
    29.         {
    30.             dir = Vector3.forward;
    31.         }
    32.     }
    33.     private void FixedUpdate()
    34.     {
    35.         if (Input.GetMouseButtonDown(0))
    36.         {
    37.             dir = new Vector3(-1f, 0, 1f);
    38.             if(transform.position.x > -4f && transform.position.x < -2f)
    39.             {
    40.                 dir = Vector3.forward;
    41.             }
    42.             rb.velocity = dir * Time.fixedDeltaTime * speed;
    43.             return;
    44.         }
    45.         if (Input.GetMouseButtonUp(0))
    46.         {
    47.             dir = new Vector3(1f, 0, 1f);
    48.             return;
    49.         }
    50.         if (transform.position.x > -1f && transform.position.x < 1f)
    51.         {
    52.             dir = Vector3.forward;
    53.             rb.velocity = dir * Time.fixedDeltaTime * speed;
    54.         }
    55.     }
    56. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,033
    Input of up/down methods ONLY work in Update(), not in FixedUpdate(). See docs for why.

    As for gradual smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    You have currentQuantity and desiredQuantity.
    - only set desiredQuantity
    - the code always moves currentQuantity towards desiredQuantity
    - read currentQuantity for the smoothed value

    Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

    The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4
     
  3. Bonchanx

    Bonchanx

    Joined:
    Mar 11, 2023
    Posts:
    4
    thank u