public void StartMoveAction(Vector3 destination)
{
GetComponent<ActionScheduler>().StartAction(this);
MoveTo(destination);
}
I saw that the StartMoveAction-Method has a void (no return value will sent) and the Method needs a Vector3 as parameter. if the Vector3 is set, theMethod can work. In the course I could not find where is the vector3 initialized? I had expected in the script following:
Vector3 destination; // set Constructor in Vairable
public void StartMoveAction(Vector3 destination)
{
GetComponent<ActionScheduler>().StartAction(this);
MoveTo(destination);
}
Where can I find the inizialation of variabel ‘destination’. In Unity’s Vector3 Contructor there are only Vector MyVector and not the word ‘destination’.
I do not know I think proper here, because I am a beginner in C#.
My script is working but I do not why…
Is there any place in Unity to link them?
thanks for your help.
I’m not familiar with the course, but the destination as required by the method parameter will get defined in the location or scope that is calling the method.
for example the update function from the same class:
void Update()
{
Vector3 dest = new Vector3(0,12, 20);
StartMoveAction(dest);
}
I used the name dest to show that it doesn’t have to be named destination.
Your second example would define the scope of destination as a class level parameter, which is possible but gets messy the more advanced your code gets. To do it that way you would access the variable with the this keyword. For example an external class may change the destination variable before StartMoveAction, which would make it hard to debug any issues.
Vector3 destination;
void Update()
{
this.destination = new Vector3(0,12, 20);
StartMoveAction();
}
public void StartMoveAction()
{
GetComponent<ActionScheduler>().StartAction(this);
MoveTo(this.destination);
}
This is a good question, Sotti, and touches on an area that can trip up new programmers easily.
We don’t actually need the class level variable Vector3 destination; as all of what’s happening in the StartMoveAction and MoveTo methods is based on the destination passed by the Controller…
In PlayerController or AIController, depending on what’s going on, the GetComponent<Mover>().StartMoveAction(destination); method is called. This is where we get the parameter destination.
In the PlayerController class, we’re raycasting to find a hit.point. This is the location that is passed into StartMoveAction(). StartMoveAction() then performs the StartAction(this) on the ActionScheduler, and then passes the destination on to the MoveTo() method.