'3d unity c# car reverse direction movement
I need some help in moving my player car in the reverse direction in unity C# scripting. The forward movement is perfect but the reverse movement is totally opposite to my expectation. I want my car to move in the reverse right direction when I press both DownArrow and RightArrow but the output I get is the reverse left direction. Correspondingly the opposite is happening when I press both DownArrow and LeftArrow. This is for my 3d game. If at all this helps, I'm using Unity 5.2.0f3(64-bit) in Windows 7
This coding also includes a few collisions.
using UnityEngine;
using System.Collections;
public class Playermovement : MonoBehaviour
{
public float speed=60;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.forward*speed*Time.deltaTime);
if(Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(0,-90*Time.deltaTime,0);
if(Input.GetKey(KeyCode.RightArrow))
transform.Rotate(0,90*Time.deltaTime,0);
if(Input.GetKey(KeyCode.DownArrow))
transform.Translate(Vector3.back*speed*Time.deltaTime);
if (Input.GetKey(KeyCode.Escape))
Application.LoadLevel("Menu");
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag=="Finish")
Application.LoadLevel("Victory");
if (other.gameObject.tag == "Destroy")
transform.Rotate(0, 180, 0);
}
}
Solution 1:[1]
You are rotating the object in a fixed orientation but you want it to be reversed when the direction is backward, then just rotate in the oposite direction if that's the case:
void Update ()
{
if(Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector3.forward*speed*Time.deltaTime);
if(Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(0,-90*Time.deltaTime,0);
if(Input.GetKey(KeyCode.RightArrow))
transform.Rotate(0,90*Time.deltaTime,0);
}
if(Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Vector3.back*speed*Time.deltaTime);
if(Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(0,90*Time.deltaTime,0);
if(Input.GetKey(KeyCode.RightArrow))
transform.Rotate(0,-90*Time.deltaTime,0);
}
if (Input.GetKey(KeyCode.Escape))
Application.LoadLevel("Menu");
}
Solution 2:[2]
Try this:
if(Input.GetKey(KeyCode.DownArrow))
transform.Translate(-Vector3.forward*speed*Time.deltaTime);
I dont know why your back translation isnt working, but see if this gives your the result you wanted. If the forward is working then simply reversing it could serve as a quick fix.
Solution 3:[3]
I like that answer by Gusman because it moved me in the right direction, but it wasn't compatible with the game I was building.
I am building the first app in the Junior Programmer series on Version 2020.3.0f1 LTS, and this is my script file PlayerController which is attached to the vehicle.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private float speed = 12.0f;
private float turnSpeed = 65.0f;
private float horizontalInput;
private float forwardInput;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector3.forward * speed * Time.deltaTime * forwardInput);
if (Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(Vector3.up, turnSpeed * horizontalInput * Time.deltaTime);
if (Input.GetKey(KeyCode.RightArrow))
transform.Rotate(Vector3.up, turnSpeed * horizontalInput * Time.deltaTime);
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Vector3.forward * speed * Time.deltaTime * forwardInput);
if (Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(Vector3.down, turnSpeed * horizontalInput * Time.deltaTime);
if (Input.GetKey(KeyCode.RightArrow))
transform.Rotate(Vector3.down, turnSpeed * horizontalInput * Time.deltaTime);
}
//We will move the vehicle forward here.
//
// transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
// transform.Rotate(Vector3.up, turnSpeed * horizontalInput * Time.deltaTime);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gusman |
Solution 2 | Jessy Doyle |
Solution 3 | Wolfpack'08 |