Simple Script in C Script

Hi guys I am making a simple driving test game.

But I am having issues with the script. here it is

using UnityEngine;
using System.Collections;

public class Drive : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if( Input.GetKey( KeyCode.W ) ) {
Transform.position += Transform.forward × 5.0f × Time.deltatime;
}
}

The issue is in the transform.position line

Thanks

Try Transform with lowercase like this : transform

Also, when you want to multiply use * and not x

Furthermore, Time.deltaTime, not Time.deltatime, proper capitalisation is crucial in coding.

OK thanks I will give it a try now

OK that worked but now I am getting the Parsing Error. I have heard about it before.

using UnityEngine;
using System.Collections;

public class Drive : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if( Input.GetKey( KeyCode.W ) ) {
transform.position += transform.forward * 5.0f * Time.deltatime;
}
}

I would bet it something very simple! But I am a first timer with this type of script

try Time.deltaTime, not Time.deltatime

I changed that but still having the same error

using UnityEngine;
using System.Collections;

public class Drive : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if( Input.GetKey( KeyCode.W ) ) {
transform.position += transform.forward * 5.0f * Time.deltaTime;
}
}

Oh I see, you forgot a closing curly brace at the end of the script, this one }

If you use tabs in your script it’s easier to read and easier to see if you miss one :slight_smile:

using UnityEngine;
using System.Collections;

public class Drive : MonoBehaviour 
{


// Update is called once per frame
	void Update () 
	{
	
		if( Input.GetKey( KeyCode.W ) ) 
		{
			transform.position += transform.forward * 5.0f * Time.deltaTime;
		}
	}
}

Thanks! That worked great BUT, I am having an issue with the transform.Rotate here is the script now

using UnityEngine;
using System.Collections;

public class Driver : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if( Input.GetKey( KeyCode.W ) ) {
transform.position += transform.forward * 5.0f * Time.deltaTime;
}
if( Input.GetKey( KeyCode.A ) ) {
transform.Rotate( 0.0f, -80.0f * Time.deltaTime, 0.0f);
}
if( Input.GetKey( KeyCode.D ) ) {
transform.Rotate( 0.0f, 80.0f * Time.deltaTime, 0.0f);
}
}
}

The ‘car’ goes in a wide circle not rotate. As it is in the tutorial. I have copied the script but it still is not doing what it should.
Do you know what I mean?

I don’t know which tutorial you mean.

I am sorry, its a short Youtube video, His ‘car’ pivots to the right and left as it should, mine goes into a large circle. The script it the same as his but still differs so there must be something I am doing wrong

Anyway, you could try this. If this isn’t it, show me the tutorial you’re talking about.

using UnityEngine;
using System.Collections;

public class Drive : MonoBehaviour 
{

	
	
	// Update is called once per frame
	void Update () 
	{
		if( Input.GetKey( KeyCode.W ) ) 
		{
			 transform.Translate(Vector3.forward * Time.deltaTime, Space.World);
		}
	
		if( Input.GetKey( KeyCode.A ) ) 
		{
			transform.Rotate( 0.0f, -80.0f * Time.deltaTime, 0.0f);
		}
	
		if( Input.GetKey( KeyCode.D ) ) 
		{
			transform.Rotate( 0.0f, 80.0f * Time.deltaTime, 0.0f);
		}
	}
}

It sounds as if the Pivot of your car’s transform is not center to the vehicle itself.

At the top of the screen are two tabs, that say [Center|Pivot] and [Global|Local]

Make sure the left one says ‘Pivot’, and move the model of your car so that it’s center on the transform of it’s parent.

Alternatively, you can set the transform’s position to 0,0,0.

OK I will give that ago

No, I am still getting the odd rotate. The ‘car’ is moving great. But the rotate is all messed up I will try to explain.

In this guy’s tut his car moves like a real car when turning. Mine moves like the hands on a clock, in a circle

It helps if you show us which tutorial you mean because if you say you did exactly what he did, how can we know what’s wrong?

He starts doing the rotate at 16:55

Ok try to attach this script to a simple cube to see what happens because that’s what I’m doing right now. There’s no need to attach a rigidbody component to try this out. My result is about the same as in the video, the cube moves, when I press A or D the cube rotates accordingly.

using UnityEngine;
using System.Collections;

public class Drive : MonoBehaviour 
{

	
	
	// Update is called once per frame
	void Update () 
	{
		if( Input.GetKey( KeyCode.W ) ) 
		{
			 transform.position += transform.forward * 5.0f * Time.deltaTime;
		}
	
		if( Input.GetKey( KeyCode.A ) ) 
		{
			transform.Rotate( 0.0f, -80.0f * Time.deltaTime, 0.0f);
		}
	
		if( Input.GetKey( KeyCode.D ) ) 
		{
			transform.Rotate( 0.0f, 80.0f * Time.deltaTime, 0.0f);
		}
	}
}

OK I will try that now.

That works fine, could it be that the shapes are not facing the right way or something? I am sure I can sort it out from here!

Thanks so much for all your help!

I’m not sure to be honest, but like JChilton said, there might be a problem with the pivot point of your 3d model (like it’s in a wrong place, so rotation looks funky).

Seems to be, but I don’t know, When I add the cube to a ‘Create Empty’ the main Cam no longer follows it, very odd as it worked fine in the last one. Also when more than one cube, or object is added to the create Empty the same rotation issue happens. IF the script has been added the create empty that is