Javascript beginner and I need some help please.

Hello, I need help with my code for my basic scene that is for college. The objective is to create a camera that moves and then create a spotlight that moves with the camera and then when you press space-bar, the spotlight centers on the cube in the center of the floor. I hate just getting an answer with an explanation as that doesn’t lead to learning, it would be greatly appreciated for any and all help. I already created the cube, the floor, the point light, camera light. I am having trouble with adding Time.deltaTime to my movement. I have code that works normally for movement, but part of the grade is to add deltaTime to it so that the player’s movement is stable and predictable. This is the code that I am working with and I am getting an error with it.

public var speed = 5.0;

function Update ()
{

var xAxis : float = Time.deltaTime * speed;
transform.Translate(Input.GetAxis(“Horizontal”),

0,

var zAxis : float = Time.deltaTime * speed;
transform.Translate(Input.GetAxis(“Vertical”)));

//transform.Translate(Input.GetAxis(“Horizontal”) xAxis,
// 0,
//Input.GetAxis(“Vertical”)
zAxis);
}

The commented out code is for the normal movement, so instead of adjusting that I made another so that incase I messed up, I would still have the original working code. Another part of my grade is to use the var zAxis and var xAxis as my new var’s. The error that I get is:

Error BCE0043: Unexpected token: var. (BCE0043) (Assembly-UnityScript) : It is for this line of code.
var zAxis : float = Time.deltaTime * speed;

I have no idea how to fix it but I have a feeling since that one is wrong, the next line is also wrong. What do I need to do to fix it?

You are declaring and initializing a variable inside of a function call for one…
You are also calling transform.Translate as a parameter to another call of transform.Translate…

I should declare xAxis and zAxis outside of the function, but then how do I update it with Time.deltaTime * speed? Do I only need to use the transform.Translate once for both vertical and horizontal then?

You’re right, that code is very messed up. What are you trying to do here?

Looks like you just copy-pasted code around hoping it would eventually work. You’ve got variable declarations calling translate twice inside a variable declaration calling translate or something crazy like that, with full statements (ending with ‘;’) inside the function call. Yikes, best to start from scratch.

I’d recommend a couple books and tutorials on how programming languages work in general.

Delete everything else you added, uncomment your old code, and just add * speed and * Time.timeDelta after the axis variables.

public var speed = 5.0;

function Update () 
{
  transform.Translate(Input.GetAxis("Horizontal") * xAxis * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * zAxis * speed * Time.deltaTime);
}

In fact I think your commented code is probably broken too, I don’t know why you would need xAxis or zAxis. You don’t declare it anywhere except for your new broken implementation of the timeDelta code.

Do this instead:

public var speed = 5.0;

function Update () 
{
  transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
}

Look how nice and clean that is. This is the thought process that would go into making that line of code.

  1. Call transform.Translate, it needs an x, y, and z value.
  2. X is Horizontal * speed * Time.deltaTime
  3. Y is 0
  4. Z is Vertical * speed * Time.deltaTime
  5. Profit!

Next time you mess up, don’t just keep trying to move things around until it works. Delete it, think logically and try again. When that doesn’t work, delete it, start writing down the things you need, build them step by step, put it together on paper, then write the code. Lather, rinse, repeat.

And really (I am serious and not trying to just attack you) just spend a few days learning a programming language. It won’t take you long to get the very basics, and after a few days of learning you will be amazed at how much easier this stuff is. You’ll be laughing and having fun so much quicker.

Thank you for the help, I see what you did there different from my jumbled mess. Only one transform.Translate is needed because of the 0 for Y axis and the comma for the same sentence for z axis. I was using the speed * Time.deltaTime wrong and I get why. That is what I do, if I mess up, I tend to move things around and try to get it to work. The class im in isn’t that great because for some reason it expects us(the students) to have some background in javascript when it’s our first time using it. The ext book isn’t even a tutorial, it’s a book about the difference of thought between object oriented programming and procedural programming. I’m trying to do my best with what is given and I wasn’t that far off the correct answer. I do understand that in scripting, even the smallest mistake can break everything. I thank for helping me a lot. What are some beginner text books or some tutorials for unity?

I’m not sure of the best beginner material for you to learn, someone else may have an idea. But you don’t need something specific to Unity to learn what you needed for this problem.

Just Google for some beginner Javascript tutorials. It won’t be exactly like the JS in Unity, but by going through a few examples of code and functions you would have known more than enough to solve this situation yourself easily.

Don’t be turned off by a JS tutorial that looks like it is just playing with text and printing out messages. That will teach you so much more about the language itself instead of trying to do fancy graphics or 3D stuff. Taking everything away and doing the simplest stuff with text output then lets you focus on what functions are, what variables are, etc.

From there you can then do scripting in Unity and let Unity do the hard 3D graphics work for you.

You can try to Google for beginner Unity scripting tutorials too, or look under this forum in the Teaching section.

I just don’t have any specific places to send you.