Search Unity

Move Player Car in Forward Direction using Physics Force

Discussion in 'Physics' started by siddharth3322, Jan 3, 2019.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    I was working on 3d car game where I want to move player car using given force. Here is gameplay snapshot:
    gameplay_screenshot.png

    At present, I have controlled the movement of a car using physics velocity.

    Here is car forward direction movement code:

    Code (CSharp):
    1.     appliedSpeed += Time.fixedDeltaTime * 7f;
    2.     appliedSpeed = Mathf.Min (appliedSpeed, speed);
    3.     myRigidbody.velocity = transform.forward * appliedSpeed;
    Here is car rotation code based on keyboard input:
    Code (CSharp):
    1.  // Rotate the car based on the control direction
    2. //     thisTransform.localEulerAngles += Vector3.up * rotateDirection * rotateSpeed * Time.deltaTime;
    3.         myRigidbody.rotation = myRigidbody.rotation * Quaternion.Euler (Vector3.up * rotateDirection * 5f);
    Now rather than velocity, I want to use physics force as per this game requirements.

    I have tried this:
    Code (CSharp):
    1. if (myRigidbody.velocity.magnitude < 5f)
    2.         myRigidbody.AddRelativeForce (transform.forward * appliedSpeed, ForceMode.Force);
    But can't able to get the desired result, its just moving into one direction only - based on car rotation, its force didn't get changed so please give me some suggestions into this.

    One important reference, I found from here:
    https://answers.unity.com/questions...facin.html?childToView=1424401#answer-1424401
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If your using transform.forward don't use the relative AddForce.

    Either
    Code (CSharp):
    1. rb.AddForce(forward * appliedSpeed);
    or
    Code (CSharp):
    1. rb.AddRelativeForce(0.0f,0.0f, appliedSpeed);
     
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    Okay I have updated my code but I was getting something like this kind of problem, please check gameplay recorded video.

    https://drive.google.com/open?id=18a6FNbZuXFtY6_wo5O0S93_SNdscQMSB
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Looks like you've done something like this:
    Code (CSharp):
    1. rb.AddForce(Vector3.Forward);
    Post updated code.
     
  5. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    No, I have moved player using this code:
    Code (CSharp):
    1.         if (myRigidbody.velocity.magnitude < 5f)
    2.             myRigidbody.AddRelativeForce (Vector3.forward * speed);
    3. //            myRigidbody.AddForce (transform.forward * speed);
    and rotated player using with this code:
    Code (CSharp):
    1.     myRigidbody.rotation = myRigidbody.rotation * Quaternion.Euler (Vector3.up * rotateDirection * 5f);
    Then after, I have recorded the video.
     
  6. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    @siddharth3322 I tried to do something on my side with a physics based object that can rotate using torque, but it never behaves correctly, always throwing the car more forward than rotating, so it's not controllable like this.

    I'm sure there has to be a better way to approach a physics car solution, not like this. Maybe with using wheel colliders it will work right, like this vid: