scenario: I’m doing a shmup
problem: I want my camera to follow a set path through a 3D environment
so how do I draw such a path for it to follow and set my camera and “invisible stopping boundaries” (so you can’t fly off screen) to follow it.
scenario: I’m doing a shmup
problem: I want my camera to follow a set path through a 3D environment
so how do I draw such a path for it to follow and set my camera and “invisible stopping boundaries” (so you can’t fly off screen) to follow it.
The scripting tutorial shows you how to get cars to drive between waypoints, which is similar to what you want your camera to do.
If you’re doing a “virtua cop” style game you could pretty much animate the camera (or its parent) using the animation timeline (i.e. give the user some kind of mouselook to point the camera) while moving the camera itself along a fixed path at a fixed rate.
Maybe you could set up a bunch of cubes as box colliders with the materials set to none(so they are invisible)down each side of your track? Make em heavy as, with perhaps a bit of bouncy so you get nudged back to the centre of the path?
I am a bit of a novice, so dont let me make life harder for you
Path following isn’t really that hard to do. If you look up the Seek and Arrive Steering behaviours (AI code, but simple), you can use those and just maintain an array of “node” positions which you can either generate at runtime, or if you like torture, type them all out by hand.
You then Seek towards the waypoint until you are within a radius of it, then you move to the next waypoint. At the last waypoint you use the Arrive behaviour (slows down and stops when it gets within the radius).
By using steering behaviours, you can smooth the motion by setting limits for speed, and more importantly, turning radius.
HTH,
-Jeremy
Just as an aside. Does anyone have any good reference sites that have example code for these type of AI behaviours? In pseudo code or generic C would be great. Or recommend any books on the subject?
cheers.
Does that work? With “none” I get a black and red object. So for invisible I had to make an empty (black) alpha channel image.
Um yeah I thought so…
Im not at my unity machine for a few days so will have to check…Am pretty sure…Under the mesh render tab(Sorry I cant help accurately for a few days)
AC
OK–chosing “none” from the list didn’t work to make an invisible object, but choosing Remove Component at the right side did.
(In fact, the object is then so invisible you can’t even see it when selected, not even in wireframe.)
To make any object invisible in the game, remove it’s renderer component from the inspector.
As for the steering behaviour code, here are Seek and Arrive in C#
//------------ Seek(GameObject target) ----------------
// Returns a velocity that points to and moves towards
// the target at a constant max speed
//-----------------------------------------------------
public Vector3 Seek(GameObject target){
Vector3 tempDir = target.transform.position - transform.position;
steeringVelocity = tempDir.normalized * maxSpeed;
return steeringVelocity;
}
//--------------- Arrive(GameObject target) -----------------
// Moves toward target but will slow down
// (set by deceleration var) as it gets close and will finally
// stop at stopPoint.
//-----------------------------------------------------------
public Vector3 Arrive(GameObject target){
Vector3 direction = target.transform.position - transform.position;
float distanceToTarget = Vector3.Distance(transform.position, target.transform.position) - stopPoint;
if (distanceToTarget <= 0.0){
steeringVelocity = Vector3.zero;
}
else {
float speed = distanceToTarget / deceleration; // apply deceleration to our speed
speed = Mathf.Min(speed, maxSpeed); // make sure our speed doesn't exceed max
steeringVelocity = direction.normalized * speed;
}
return steeringVelocity;
}
Now all that code does in calculate and return a velocity vector for that frame (you call them every frame that you want them to compute). I obviously left out some variable declarations, but I figure you are going to convert this to JS anyway.
Now, to move your object you have two choices. If you object has a rigidbody, you can use this code to move it using the steering behaviour
int Locomotion(Vector3 velocity){
targetRotation = Quaternion.LookRotation(velocity.normalized);
str = Mathf.Min(rotationSpeed * Time.deltaTime, 1);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, str);
rigidbody.velocity = transform.TransformDirection(Vector3.forward) * (velocity.magnitude);
return 0;
}
You just call it (in C#) every frame like this:
Locomotion(Seek(target));
But, if you dont have a rigidbody attached you obviously cant move it with rigidbody.velocity, so you use this formula to apply velocity on your own:
//use this formula to apply velocity
position += velocity * Time.deltaTime;
//so the rigidbody line above becomes:
transform.position += (transform.TransformDirection(Vector3.forward) * (velocity.magnitude)) * Time.deltaTime;
The locomotion code will slowly turn towards the next target, therefore providing some smoothing.
HTH,
-Jeremy