Trying to understand scripting, disecting a script

Hi, I’m took the Click To Move javascript from Unify Wiki site and got it to work in Unity, But then I’ve been going through the whole script and dissecting it as much as I can. I have put in comments to describe what understanding I put into each codeline, could someone point out to me my rights and wrongs :slight_smile:

// Click To Move script
// Moves the object towards the mouse position on left mouse click

var smooth:float; // Determines how quickly object moves towards position
// var is a variable, should be open since it's outside of any function. 
// float = a numeric value (float is not limited to whole numbers, so a fraction like 0.5 works fine)
// as far as I understand the : symbol works the same way as the = symbol but I have still noticed differenses where one symbol gives me an error but the other doesn't

private var targetPosition:Vector3;
// private var is as far as I can understand the same as var, but making it private means it's hidden from Unity's UI even if it's outside of any function.
// the value of the  targetPosition variable is a Vector3, wich to my understanding is a point in 3d space with position values on the 3 different axes (X,Y,Z)

function Update () 
// a function Update is a function that is called/executed once every frame... everything inside the brackets { } is the function's orders (what the function does)
// not entirely sure about how the () symbols work in this case and what code I could put inside it and to what purpose
{
    if(Input.GetKeyDown(KeyCode.Mouse0))
	//if the stuff inside the () symbols is true, only then will the script go on and execute the following code inside the brackets { } 
	// Input.GetKeyDown the input is a signal sent from the computer hardware (pre defined keystrokes or mouseclicks ans whatnot) and Input.GetKeyDown means that as soon as the inputsignal/button is clickedDown
	//(KeyCode.Mouse0) is the code for describing the leftmouse  button.
	//so this code so far is asking/checking every frame if the leftmouse button is held down or not, if it is true it executes the following code.
    {
		var playerPlane = new Plane(Vector3.up, transform.position);
		// new is I'm guessing a code for creating something, like a gameobject... but I cannot find any more info on it when searching the documentation for the word "new"
		//Plane is a gameObject that we are creating and it needs me to specify wich way it is facing (normal) and then the distance from the planes origin
		//the (Vector3.up) means that the plane we are creating is facing up (or towards the positive Y axis).
		// the (transform.position) is telling what position the origin of the plane is, more than that I'm not sure how it knows the size of the plane.
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		// a variable wich sends a ray from the screen(camera) and then we define further inside the () symbols that we shot the ray out from the mouseposition.
        var hitdist = 0.0;
		// here we are creating a numerical float variable called hitdist
		
       
        if (playerPlane.Raycast (ray, hitdist)) 	
		//another if statement, checking for raycasting on the playerPlane variable
		// it checks if there is a ray variable (the ray shot from our camera(mouseposition))
		// then it checks if we have the hitdist variable in there, as far as I understand we defined the hitdist variable before to be a float, but what this float is telling the Raycast Class I'm not sure off or maybe it's just enough that it's there, for it to continue on with the code.
		{
            var targetPoint = ray.GetPoint(hitdist);
			//a new variable that if I understand correctly, gets the 3dvector (XYZcoordinates) from where the ray (cast from the camera/mouseposition) hit the plane we created earlier
			//then it uses the hitdist for whatever purpose, I don't understand what it is doing.
            targetPosition = ray.GetPoint(hitdist);
			//changes the targetPosition variable to the same 3dvector described above (targetPoint). why we would need both of them I have no idea, since I would suspect at this stage they are identical.
            var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
			//uses Quaternion calculations to calculate the angle between the current position to the targetPoint.
            transform.rotation = targetRotation;
			//calls up the rotation of the gameObject and puts in the targetRotation information there to tell the object to actually turn in this direction.
        }
    }
   
    transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
	//calls up the transform,position of the object then using the Vector3.Lerp Class we tell it to go from the vector of transform.position (current position) to the vector of the targetPosition variable, then we tell it how long it should take to do so, and we tell it to do it in deltaTime and then multiply it by the smooth variable.
}

Most of your understanding seems to be correct, so I’m only including the parts that I believe require some clarification:

// as far as I understand the : symbol works the same way as the = symbol but I have still noticed differenses where one symbol gives me an error but the other doesn't

The : symbol defines the type of the variable (‘float’ would be a number that could any number including fractions, ‘int’ would be a natural number, no fractions allowed. there are also completely different types, like a ‘string’ which would be a sequence of text or vector3 which defines a position in space.), whereas ‘=’ defines the value of the variable, where a float could be 1.4, an int could be 1023 and a string could be “parrot”.

function Update () 
// a function Update is a function that is called/executed once every frame... everything inside the brackets { } is the function's orders (what the function does)
// not entirely sure about how the () symbols work in this case and what code I could put inside it and to what purpose

The () symbols can include possible parameters of the function, as some functions need input information to do their job. A function that would calculate the square of a number, would look like this:
function square(number : float)
{
return number * number;
}
Even if a function doesn’t need any parameters (like Update), you still need to use the enclosing brackets or the compiler will complain.

		var playerPlane = new Plane(Vector3.up, transform.position);
		// new is I'm guessing a code for creating something, like a gameobject... but I cannot find any more info on it when searching the documentation for the word "new"
		//Plane is a gameObject that we are creating and it needs me to specify wich way it is facing (normal) and then the distance from the planes origin
		//the (Vector3.up) means that the plane we are creating is facing up (or towards the positive Y axis).
		// the (transform.position) is telling what position the origin of the plane is, more than that I'm not sure how it knows the size of the plane.

Mostly correct. A Plane is not a GameObject, though. Like Vector3 it is a mathematical construct that isn’t actually visible within your hierarchy window. It behaves much like an int or float do, as something you do calculations with.

As for its size, planes in mathematics are infinite in size. This is also true in Unity, your plane is facing up, originates in the player position, and stretches along infinitely.

        if (playerPlane.Raycast (ray, hitdist)) 	
		//another if statement, checking for raycasting on the playerPlane variable
		// it checks if there is a ray variable (the ray shot from our camera(mouseposition))
		// then it checks if we have the hitdist variable in there, as far as I understand we defined the hitdist variable before to be a float, but what this float is telling the Raycast Class I'm not sure off or maybe it's just enough that it's there, for it to continue on with the code.

hitdist is a so-called ‘output parameter’. The function raycast will return true if the plane is hit, but it will also ‘fill’ the hitdist variable with distance information that allows you to calculate where the ray hit the plane exactly.

  var targetPoint = ray.GetPoint(hitdist);
			//a new variable that if I understand correctly, gets the 3dvector (XYZcoordinates) from where the ray (cast from the camera/mouseposition) hit the plane we created earlier
			//then it uses the hitdist for whatever purpose, I don't understand what it is doing.

Indeed, GetPoint is the function that does the calculation where the ray hits the plane. It needs to know the value of hitdist (as provided by the raycast) to get the correct result.

     targetPosition = ray.GetPoint(hitdist);
			//changes the targetPosition variable to the same 3dvector described above (targetPoint). why we would need both of them I have no idea, since I would suspect at this stage they are identical.

Correct. This line makes no sense at all. targetPosition and targetPoint are identical from here on. Since targetPosition isn’t used inside this script anymore, it’s only purpose is to expose the value as a variable (remember, it is defined on top). Why two different variables are used for this is beyond me. You could replace all ‘targetPoint’ by ‘targetPosition’ and then remove one of these assignments to get the exact same script behavior.

    transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
	//calls up the transform,position of the object then using the Vector3.Lerp Class we tell it to go from the vector of transform.position (current position) to the vector of the targetPosition variable, then we tell it how long it should take to do so, and we tell it to do it in deltaTime and then multiply it by the smooth variable.
}

Not quite. Using Lerp does move from the first position to the second (Lerp stands for Linear intERPolation, believe it or not), however the way it is used here is slightly more complicated than that. Whithout going into the maths behind it: using this type of Lerp with Time.deltaTime will make your object move from it’s current position to the target position, moving fast at first and then slowing down when getting closer to the target.

Oops, waited too long to post this before Tom did. :wink: Anyway…

Not sure what you mean by “open”.

Not at all. : is for defining the type of a variable. = is for assigning a value to a variable. In JS (and C# 3.0 and later) you can have the compiler infer the type when you assign a variable, however : and = are two distinctly different concepts.

Not really…you can unhide the variable by using debug mode in the inspector. It’s more for preventing other classes from having access to that variable; being hidden in non-debug mode is sort of a side effect. @HideInInspector will actually hide the variable in the UI, without affecting the public or private aspect.

The () are for parameters, which Update doesn’t have, but you still need them whether a function actually has any parameters or not.

You can generally leave “new” out in JS; it’s optional.

No, it’s checking for a raycast hit. The hitdist variable is for the distance parameter of the raycast function.

It’s for the distance parameter of the GetPoint function.

Yeah, it would be better just to say “targetPosition = targetPoint;” instead of wasting time calling the GetPoint function twice for the same result. Or maybe it could be simplified even more; I haven’t really tried to follow the code.

That’s not what Lerp actually does…it just returns a value immediately, where the result is somewhere between the first and second parameters, depending on the third parameter, which is clamped between 0 and 1. See here.

–Eric

Thanks a lot !!! incredible feedback guys, I wasn’t sure anyone would bother to answer this since it’s actually a lot of questions in one go. Thanks to you my scripting understanding went from “blind guessing” to “something I can build upon” :slight_smile: