- Home /
Car Script error C#
Hello guys I was trying to make a script for my car in my game but every time my car rotates it goes sideways like if the z axis didn't rotate with the object
the script goes like this:
using UnityEngine;
using System.Collections;
public class CarControl : MonoBehaviour {
public bool Driveble = false;
public bool isTouching = true;
public float MaxAcelerationForwards = 0.0f;
public float MaxAcelerationBackwards = 0.0f;
private float currentacelerationF = 0.0f;
private float currentacelerationB = 0.0f;
private float Px;
private float Py;
private float Pz;
private bool AceleratingForwards = false;
private bool AceleratingBackwards = false;
void Update () {
if(Driveble){
if(Input.GetKey(KeyCode.W))
{
Px = transform.position.x;
Py = transform.position.y;
Pz = transform.position.z;
if(isTouching){
if(currentacelerationF < MaxAcelerationForwards){
currentacelerationF ++;
transform.position = new Vector3(Px, Py, Pz += currentacelerationF * Time.deltaTime);
}
else{}
}
else{}
}
else{
if(Input.GetKey(KeyCode.S))
{
Px = transform.position.x;
Py = transform.position.y;
Pz = transform.position.z;
if(isTouching){
if(currentacelerationB < MaxAcelerationBackwards){
currentacelerationB++;
transform.position = new Vector3(Px,Py,Pz -= currentacelerationB * Time.deltaTime);
bool AceleratingForwards = true;
}
else{}
}
else{}
}
else{
if(Input.GetKey(KeyCode.A))
{
}
else{
if(Input.GetKey(KeyCode.D))
{
}
else{
if(AceleratingForwards == false){
if(currentacelerationF < MaxAcelerationForwards){
if(currentacelerationF > 0.0f){
Px = transform.position.x;
Py = transform.position.y;
Pz = transform.position.z;
currentacelerationF--;
transform.position = new Vector3(Px,Py,Pz += currentacelerationF * Time.deltaTime);}
}
else{
if(currentacelerationF == MaxAcelerationForwards){
if(currentacelerationF > 0.0f){
Px = transform.position.x;
Py = transform.position.y;
Pz = transform.position.z;
currentacelerationF--;
transform.position = new Vector3(Px,Py,Pz += currentacelerationF * Time.deltaTime);}
}
}
}
else{
if(AceleratingForwards == false){
if(currentacelerationB < MaxAcelerationBackwards){
}
else{
if(currentacelerationB == MaxAcelerationBackwards){
}
}}
}
}
}
}
}
}
}
}
if you guys could help me I would be grateful.
By the way I haven't finished yet so there's stuff missing.
just FTR
it is very, very, very, very, very, very difficult to build car rigs.
there is a FULL tutorial of a race car on an island, which unity gives for free. you can read through the 100s and 100s of pages of PDF explaining (very basically) how to make a car move in a video game.
here .. http://unity3d.com/support/resources/tutorials/car-tutorial
download the three TEN $$anonymous$$EGABYTE documentation files and start reading !
youc an also buy pretty goo car rigs for a few dollars o the asset store.
Answer by ghhwer · Oct 27, 2012 at 10:42 AM
Thank you all but i figured out how to do its just change the lines were I wrote
transform.position = new Vector3(Px,Py,Pz += currentacelerationF * Time.deltaTime);}
to
transform.Translate(Vector3.forward * currentacelerationF * Time.deltaTime);
but still thanks for your help!
Answer by hvilela · Oct 26, 2012 at 12:19 PM
Well, there's absolutely no rotate code there and the rotation won't change unless you change it. Check transform.rotation.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I make sure two movable objects stay a certain distance away from eachother? 0 Answers
I need help with a script(brauche hilfe mit einen Script) 0 Answers