Slowing down change in Vector 3 transform position.

Hi, after plenty of kind help from the people here, I finally got a code problem to work. Thankyou!

The only missing quality from it is that the movement of the enemy from one Vector 3 position to another is too fast.

Is there anyway to make it move more slowly from one position to another?

void diveAtPlayer ()
    {
        transform.position = new Vector3(transform.position.x, player.position.y+2, transform.position.z);
        }

lerp lets you work out a mid point between the start and end points based on how much time has past (i.e. deltaTime).

Lerp takes a value between 0 and 1 and should not be used with delta time by itself.

Instead, try Vector3.MoveTowards.

1 Like

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.

I tried this,

transform.position = Vector3.MoveTowards(transform.position.x, player.position.y+2, transform.position.z);

then I get these errors,

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?

Vector3.MoveTowards(new Vector3(<...values...>));

you’re trying to create a new vector3 but haven’t told the code that :slight_smile: 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

OK, great. When I try to type that:

transform.position = Vector3.MoveTowards (new Vector3(transform.position.x, player.position.y+2, transform.position.z));

I get one error:

Assets/Level3Scripts/EyeBird.cs(50,46): error CS1501: No overload for method MoveTowards' takes 1’ arguments
Is it meant to be typed more like this:

transform.position = Vector3.MoveTowards (new Vector3(<transform.position.x, player.position.y+2, transform.position.z>));

Or this:

transform.position = Vector3.MoveTowards (new Vector3(<x, y+2, z>));

Sort of wondering about the combinations…

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 Vector3 MoveTowards(Vector3 current, Vector3 target, 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 new Vector3(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).

I think a Tweening engine would be good for this.

This code works pretty well. Thanks Duugu.

transform.position = Vector3.MoveTowards (this.transform.position, new Vector3(1,2,3), 0);

The 0 at the close of the bracket, is that the position the Object with the code on it starts in?

(this.transform.position, new Vector3(1,2,3), 0)

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.

1 Like