- Home /
Question by
Ammar91 · Nov 13, 2018 at 09:16 AM ·
movementmovement scriptcarorientation
How to make the object rotation affect the control of the object
hi, I'm trying to make car movement, and I accomplish moving forward and backward, and I made the movement to the right and left as rotation to the right and left. the issue is when I rotate to right of left; controls of the car stays forward and backward along the x axis. any ideas? here is the code
using UnityEngine;
public class movement : MonoBehaviour {
public Rigidbody car;
// Use this for initialization
void Update () {
noMove:
if( Input.GetKey("w"))
{
car.AddForce(0, 0, -500 * Time.deltaTime);
}
if (Input.GetKey("s"))
{
car.AddForce(0, 0, 500 * Time.deltaTime);
}
move:
if (Input.GetKey("d"))
{
car.transform.Rotate(Vector3.up * 60*Time.deltaTime, Space.World);
}
if (Input.GetKey("a"))
{
car.transform.Rotate(Vector3.down * 60*Time.deltaTime, Space.World);
}
}
}
Comment
Best Answer
Answer by Ammar91 · Nov 15, 2018 at 01:27 PM
I manage to solve this issue by using the function transform.forward here is the code:
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
public Rigidbody ball;
// Use this for initialization
void Update()
{
//for controling the car
if (Input.GetKey("up"))
{
ball.AddForce(transform.forward*(-100)*Time.deltaTime);
if (Input.GetKey("right"))
{
ball.transform.Rotate(Vector3.up * 35 * Time.deltaTime, Space.World);
}
if (Input.GetKey("left"))
{
ball.transform.Rotate(Vector3.down * 35 * Time.deltaTime, Space.World);
}
}
if (Input.GetKey("down"))
{
ball.AddForce(transform.forward*100*Time.deltaTime);
if (Input.GetKey("right"))
{
ball.transform.Rotate(Vector3.up * 35 * Time.deltaTime, Space.World);
}
if (Input.GetKey("left"))
{
ball.transform.Rotate(Vector3.down * 35 * Time.deltaTime, Space.World);
}
}
}
}
Your answer
Follow this Question
Related Questions
How to make a car move forward indefinitly ? 2 Answers
Implementing Counter-Movement 0 Answers
How do I make an object move and have an animation attached at the same time? 0 Answers
Touch buttons for step movememt 3 Answers
Accelerometer 2 Answers