Search Unity

Align Rigidbody on Rail?

Discussion in 'Physics' started by RobertOne, Oct 12, 2016.

  1. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    hello! i just wanted to create a small carrera rc slotcar kinda game. but i stuck at the part where the car should drive in a curve. the code looks simple as this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Carrera : MonoBehaviour {
    4.      public float accerleration;
    5.      void Update () {
    6.          if (Input.GetKeyUp(KeyCode.DownArrow)) {
    7.              this.gameObject.GetComponent<Rigidbody>().AddForce(this.gameObject.transform.forward * accerleration, ForceMode.Acceleration);
    8.    
    9.          }
    10.          if (Input.GetKey(KeyCode.DownArrow)) {
    11.              this.gameObject.GetComponent<Rigidbody>().AddForce(this.gameObject.transform.forward * -accerleration, ForceMode.Acceleration);
    12.          }
    13.    
    14.      }
    15. }
    16.  
    but the problem with this is that:
    rigidbody_rail2.gif


    i somehow have to tell the rigidbody that he should rotate to the left in the curve or something like that. but how?
     
  2. TWicked

    TWicked

    Joined:
    Nov 8, 2013
    Posts:
    14
    Rotate RB in direction of moment
    transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
    i think above will work.
    .