so i have an enemy which straight up follows the player but i also want an enemy that still follows the player but not straight. it should make some weird wiggly movement or something. but i dont want to invest too much effort with a pathfinding system or so… is there an easy way? maybe just increase the transform.y by 2 every second? i know that i should make a coroutine to reach the “every second” but i dont know how to set it. i tried it with:
You’re not really doing anything with what you have. If you want to set the transform.position, then you’ll need to do something like the code here.
While this is adding a vector3 to a vector3, the idea is simply to capture the current position, increment or change whatever value you want, then set that value back to the transform.position.
The reason this doesn’t work on transform.position is because transform.position is a property. A property is a pair of get/set functions that masquerade as a variable to simplify code and make it more readable; this way you can do logic upon getting and setting the value. So inside the code for the Transform class, there is some code that vaguely looks like this (obviously the Transform class does a lot more with this, but this is all that’s relevant here):
private Vector3 internalPosition = Vector3.zero;
public Vector3 position {
get {
return internalPosition;
}
set {
internalPosition = value;
}
}
So you can use this property like a variable, right? Well… not quite. It helps to think of the property as two separate functions (which it is, behind the scenes):
public Vector3 GetPosition() {
return internalPosition;
}
public void SetPosition(Vector3 value) {
internalPosition = value;
}
So now try to do what you’re doing in your code, and it should be easier to see why it isn’t working:
transform.GetPosition().y += 10f;
Notice that there is no call to SetPosition here. GetPosition simply returns a copy of the position Vector3. You’re then modifying the y of that property, and then doing nothing with that modified value.
So you have to call the equivalent of SetPosition, which means you have to use “.position = something” at some point basically. So here’s what you end up with:
var myPos = transform.position;
myPos.y += 10f;
transform.position = myPos;
Thank you for the great answer!
But i’m still confused. If i use it like that, the enemy doesn’t move to my player anymore. i tried to safe the current position, edit Y by 10 or something, wait for 1 sec and reset the position again. that clearly doesnt work.
then i tried to make a bool that controls if the function can be activated (if its true, the coroutine will start, if its false, the coroutine doesnt start and the enemy will just follow the player) in the coroutine i change the position by 10, wait for 1sec, subtract the position.y by 10, wait again for 1 sec and set the bool to true again but that also doesnt work. i think its because i call the coroutine in update but i dont know how i should do it instead bc when i dont call it in update, the enemy won’t follow the player anymore…