So I’m working on a fly AI, I started by creating basic functions for things like moveRight(), moveLeft(), moveUp(), moveDown(). These are incredibly useful and functional movements, but the only thing I dont get out of these is a rounded movement. They are all straight lines, combined functions like my diagMoveRightUp() which is simply moveRight(); moveUp(); work fantastically for a diagonal line. So using transform.position methods what would be a good way to make a curve style movement or archs?
Edit: This is a 2D game just fyi.
In kinematic motion, archs and curves are usually the result of acceleration being applied to some velocity. Eg: An object sailing through the air is accelerated by gravity, which causes it’s path to look like a quadratic function.
This example shows an object that is moving up and to the right, before gravity pulls it back down in a quadratic arc.
void Start() {
// This would be 50 meters per second to the right,
// 100 meters per second upwards.
velocity = new Vector2(50, 100);
}
void Update() {
// Apply gravity to velocity
velocity += Physics.gravity * Time.deltaTime;
// Move the object according to it's velocity
transform.position += velocity * Time.deltaTime;
}
Vector2 velocity;
EDIT: If you can somehow restrict the way the player can move every time, you may be able to set up a limited number of curves in advance. If you want freedom of movement calculated through code, there’s gonna be a bit of math and physics involved in getting your curves!
It sounds like you might be working on a SHMUP. If you are, then I might do something like this.
/// Maximum left-right and up-down speed.
float speed { get; set; }
void Update() {
Vector2 velocity;
velocity.x = Input.GetAxis("Horizontal");
velocity.y = Input.GetAxis("Vertical");
velocity *= speed;
transform.position += velocity * Time.deltaTime;
}
EDIT: Ah so I’m reading this post again and I realize it’s for AI. Instead of using Input.GetAxis(), you can come up with your own Vector2 velocity value any way you want. (The easiest two I can think of are fly towards the player, and fly away from the player.)
This is perfect. A great start! Thank you Garth, it’s very much appreciated.