S99 Posted March 21, 2013 Share Posted March 21, 2013 Hi. I am making a game in unity. It is a simple driving game and i intend to make it a PC download and a Android game. I am making the car etc.. but need help I am writing the code (C#) so far i can make the car go forward. but can not make it turn code so far... using UnityEngine;using System.Collections;public class Car: MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () { if(Input.GetKey(KeyCode.W)) { transform.position += transform.forward * 5.0f * Time.deltaTime; } }} if you can help message me I am currently working on a website so... Hope you can help Link to comment Share on other sites More sharing options...
Rawra Posted March 21, 2013 Share Posted March 21, 2013 (edited) Hi I don't know how much help I will be as I don't use C#, but what is transform.position? A vector? If so, you would want to manipulate the individual elements (x,y ) separately such as : if(Input.GetKey(KeyCode.W)) { transform.position.y += transform.forward * 5.0f * Time.deltaTime;if(Input.GetKey(KeyCode.A)) { transform.position.x += transform.right* 5.0f * Time.deltaTime; or something along those lines Edit: Sorry, I thought you wanted it to MOVE right, not rotate Edited March 21, 2013 by Rawra Link to comment Share on other sites More sharing options...
S99 Posted March 21, 2013 Author Share Posted March 21, 2013 Hi I don't know how much help I will be as I don't use C#, but what is transform.position? A vector? If so, you would want to manipulate the individual elements (x,y ) separately such as : if(Input.GetKey(KeyCode.W)) { transform.position.y += transform.forward * 5.0f * Time.deltaTime;if(Input.GetKey(KeyCode.A)) { transform.position.x += transform.right* 5.0f * Time.deltaTime; or something along those lines Right. UPDATE Working script using UnityEngine;using System.Collections;public class Car : MonoBehaviour {public float forwardSpeed = 5.0f;public float backwardSpeed = -2.0f;public float turnRate = 80.0f;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () { if(Input.GetKey (KeyCode.S)){ transform.position += transform.forward * backwardSpeed * Time.deltaTime; } if(Input.GetKey (KeyCode.W)){ transform.position -= transform.forward * forwardSpeed * Time.deltaTime; } if(Input.GetKey (KeyCode.A)){ transform.Rotate (0.0f, -turnRate * Time.deltaTime, 0.0f); } if(Input.GetKey (KeyCode.D)){ transform.Rotate (0.0f, turnRate * Time.deltaTime, 0.0f); }} } That is the script for moving the car. A little guide. Transform.position if for the movement of the car mainly for things like a auto programed thing staying a certan distance from a tree. public float is so you can edit the variables. And thanks for the help Rawra Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now