Thanks for the feedback people. I think that may give me enough to go on. Iâm interested in Lerps, Slerps, Enums, Inumerators, Switch Statements, State Machines. It must be great to write code from scratch. I mainly patchwork public examples at the moment.
Assets/Level3Scripts/EyeBird.cs(51,46): error CS1503: Argument #1' cannot convert floatâ expression to type UnityEngine.Vector3' Assets/Level3Scripts/EyeBird.cs(51,46): error CS1502: The best overloaded method match for UnityEngine.Vector3.MoveTowards(UnityEngine.Vector3, UnityEngine.Vector3, float)â has some invalid arguments
Thereâs some monodevelop suggestion that says âpublic static Vector3 MoveTowards (Vector3 current, Vector3 target, float maxDistanceDelta)â
Do I need to declare these variables at the start?
youâre trying to create a new vector3 but havenât told the code that the movetowards function doesnât have a 3 floats version, so you need to create the vector3 with ânewâ and pass that into the function
Youâre only specifying a single position in your call to Vector3.MoveTowards. You need 3 arguments for that function.
The first argument is your current position (transform.position), the second the target position (the one you seem to be calculating) and a step distance (how much to move towards target this frame).
The key here is that youâre going to need to calculate just how big a step to take this frame. Youâll need a concept of speed for this to work. The documentation has a good example of how to do this : Unity - Scripting API: Vector3.MoveTowards
MoveTowards takes 3 parameters:
public static Vector3MoveTowards(Vector3current, Vector3target, float maxDistanceDelta);
The new Vector3() statement creates a new value (a Vector3) in memory (a new instance of the Vector3 class to be exact). It takes 3 parameters too.
public Vector3(float x, float y, float z);
Consider the following examples:
Vector3 aSampleVector = new Vector3(1, 2, 3);
this.transform.position = aSampleVector;
----
this.transform.position = new Vector3(1, 2, 3);
Theyâre doing the same. Creating a new Vector3 value with (x=1, y=2 and z=3). The ony difference is, the first example assigns the value to a variable and then the variable to the position, and the second example directly assigns the new value to the position.
So the code
transform.position = Vector3.MoveTowards (new Vector3(1,2,3));
creates a new Vector3 value (thatâs the newVector3(1,2,3) part) which is directly passed as the first Parameter to Vector3.MoveTowards.
But Vector3.MoveTowards needs 3 parameters (position, target, distance). Like
transform.position = Vector3.MoveTowards (this.transform.position, new Vector3(1,2,3), 0);
(donât use the values from my example, read the MoveTowards documentation).
No, that last parameter is the amount (distance) you want to move on this frame. 0 makes no sense (probably why @Duugu said not to use his values, but to instead read the docs).
And Iâll second that. You seem to be trying to piece together something out of snippets pasted here, without understanding what youâre doing at all. If you read the docs, as has been suggested several times, you will not only have a chance of understanding what youâre doing â youâll also have a sample code thatâs very close to what youâre trying to do.