- Home /
Make an object, with a random movment, flip
Hello Everyone (:
I'm learning how to code, and figuring how to do certain stuff alone is hard sometimes, so i come here to ask some help:
I'm trying to make fish change its direction every 3 seconds. This I managed to do but, i want the fish to flip if he change his direction left to right and to left again, and this i can't do it
I tough that i could store his previous position, his new position and compare. If the oldPosision.x is lower than newPosition.x, than it should flip. But i'm having some trouble to store the positions. I tried diferent metods, and the best that i could do is store the new position, but not the old position.
So anyone can help me? how can i make this fish flip? My logic is right? There are better ways? what is the best way in your opinion?
Thank you for your time (:
I'm having difficulty understanding your question. In particular, this line is confusing for me:
i want the fish to flip if he change his direction left to right and to left again, and this i can't do it
Answer by JayOhhh · Jun 19, 2014 at 09:55 PM
Create a variable with the fish's flip positions.
private Vector3 fishFlipRight= new Vector3(x,x,x);
private Vector3 fishFlipLeft = new Vector3(x,x,x):
public GameObject fishPrefab; // attach fish prefab through inspector
void update(){
if (fishPrefab.transform.position == fishFlipRight) {
//Use transform.rotate to achieve flipping motion.
}
if (fishPrefab.transform.position == fishFlipLeft ) {
//Use transform.rotate to achieve flipping motion.
}
}
Might need a little bit of tweaking (hint, booleans for isFlipped), in-case your fish is moving slowly and sits in the start/end position for a long time, but this should be the general logic.