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 ![]()
// 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.
}