- Home /
object rotates when moving backward
whenever i press s to move my vehicle backwards it also rotates it to the right, this is a script i made ages ago when i was new to unity and i cant see anything wrong.
var weapon = 0;
var m = 1;
var f = 2;
var c = 3;
var machinegun1 : GameObject;
var machinegun2 : GameObject;
var flamethrower : GameObject;
var cannon: GameObject;
var tractor1cube : GameObject;
var forwards : float = 0;
var turn : float = 0;
var maxSpeed = 8;
var minSpeed = -6;
var maxTurn = 135;
function Update () {
maxTurn = forwards / 6 * 135 + 20;
if (weapon >= 1)
Invoke("WeaponSelect",0);
if (weapon >= 1){
transform.Rotate(Vector3.up * Time.deltaTime * turn);
}
if (weapon >= 1){
transform.Translate(Vector3.left * Time.deltaTime * forwards);
}
if(turn >= 0){
turn -= 3.25;
}
if(turn <= 0){
turn += 3.25;
}
if(forwards <= 0){
forwards += 0.2;
}
if(forwards >= 0){
forwards -= 0.1;
}
if(forwards >= maxSpeed){
forwards = maxSpeed;
}
if(forwards <= minSpeed){
forwards = minSpeed;
}
if(turn >= maxTurn){
turn = maxTurn;
}
if(turn <= -maxTurn){
turn = -maxTurn;
}
if (Input.GetKey("a")) {
turn -= 7.5;
if(forwards >= maxSpeed / 2){
transform.Translate(Vector3.forward * Time.deltaTime * turn / 50);
}
if(forwards >= maxSpeed * 0.75){
forwards -= 0.6;
}
}
if(Input.GetKey("d")) {
turn += 7.5;
if(forwards >= maxSpeed / 2){
transform.Translate(Vector3.forward * Time.deltaTime * -turn / 50);
}
if(forwards >= maxSpeed * 0.75){
forwards -= 0.6;
}
}
if (Input.GetKey("w")) {
forwards += 0.3;
}
if (Input.GetKey("s")) {
forwards -= 0.5;
}
}
function OnGUI () {
if (weapon < 1){
if (GUI.Button (Rect (Screen.width * 0.02,60,160,120), "Guns")) {
weapon = m;
Destroy (tractor1cube);
}
}
if (weapon < 1){
if (GUI.Button (Rect (Screen.width * 0.02,220,160,120), "Flamethrower")) {
weapon = f;
Destroy (tractor1cube);
}
}
if (weapon < 1){
if (GUI.Button (Rect (Screen.width * 0.02,380,160,120), "Cannon")) {
weapon = c;
Destroy (tractor1cube);
}
}
}
function WeaponSelect() {
if (weapon != m)
Destroy (machinegun1);
if (weapon != m)
Destroy (machinegun2);
if (weapon != f)
Destroy (flamethrower);
if (weapon != c)
Destroy (cannon);
}
also with the
if(forwards >= maxSpeed / 2){
transform.Translate(Vector3.forward * Time.deltaTime * turn / 50);
}
part if i rotate it right it drifts around a point in front of it but if i rotate left it drifts around a point behind it, does anyone know why or how to fix it?
Answer by sparkzbarca · Nov 10, 2012 at 09:30 AM
what happens if there going slow (less than half speed)
if (Input.GetKey("a")) { turn -= 7.5; if(forwards >= maxSpeed / 2){ transform.Translate(Vector3.forward Time.deltaTime turn / 50); }
what happens if forwards is less than maxspeed/2 apparently slow moving tanks dont turn?
the positive and negative sign probably change the vector to point in front or behind changing what your rotating around.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083534im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving objects in game using .transform does not stop 3 Answers
rotare arrow object to mouse position 0 Answers
rot not working for 2nd prefab 2 Answers
object rotating around parent 1 Answer