Imagine that we're developing a cool action game where the player is capable of escaping using cliffs and rooftops. In that case, the enemies need to be able to chase the player and be smart enough to discern whether to take the jump and gauge how to do it.
Creating a jump system
Getting ready
We need to create a basic matching-velocity algorithm and the notion of jump pads and landing pads in order to emulate velocity math so that we can reach them.
The following is the code for the VelocityMatch behavior:
using UnityEngine; using System.Collections; public class VelocityMatch : AgentBehaviour { public float timeToTarget = 0.1f; public override Steering GetSteering() { Steering steering = new Steering(); steering.linear = target.GetComponent<Agent>().velocity - agent.velocity; steering.linear /= timeToTarget; if (steering.linear.magnitude > agent.maxAccel) steering.linear = steering.linear.normalized * agent.maxAccel; steering.angular = 0.0f; return steering; } }
Also, it's important to create a data type called JumpPoint:
using UnityEngine; using System.Collections; public class JumpPoint { public Vector3 jumpLocation; public Vector3 landingLocation; //The change in position from jump to landing public Vector3 deltaPosition; public JumpPoint () : this (Vector3.zero, Vector3.zero) { } public JumpPoint(Vector3 a, Vector3 b) { this.jumpLocation = a; this.landingLocation = b; this.deltaPosition = this.landingLocation - this.jumpLocation; } }
How to do it...
- Create the Jump script along with its member variables:
using UnityEngine; using System.Collections; public class Jump : VelocityMatch { public JumpPoint jumpPoint; public float maxYVelocity; public Vector3 gravity = new Vector3(0, -9.8f, 0); bool canAchieve = false; }
- Implement the SetJumpPoint function:
public void SetJumpPoint(Transform jumpPad, Transform landingPad) { jumpPoint = new JumpPoint(jumpPad.position, landingPad.position); }
- Add a function to calculate the target:
protected void CalculateTarget() { target = new GameObject(); target.AddComponent<Agent>(); target.transform.position = jumpPoint.jumpLocation; //Calculate the first jump time float sqrtTerm = Mathf.Sqrt(2f * gravity.y * jumpPoint.deltaPosition.y + maxYVelocity * agent.maxSpeed); float time = (maxYVelocity - sqrtTerm) / gravity.y; //Check if we can use it, otherwise try the other time if (!CheckJumpTime(time)) { time = (maxYVelocity + sqrtTerm) / gravity.y; } }
- Implement the CheckJumpTime function, to decide whether it's worth taking the jump:
private bool CheckJumpTime(float time) { //Calculate the planar speed float vx = jumpPoint.deltaPosition.x / time; float vz = jumpPoint.deltaPosition.z / time; float speedSq = vx * vx + vz * vz; //Check it to see if we have a valid solution if (speedSq < agent.maxSpeed * agent.maxSpeed) { target.GetComponent<Agent>().velocity = new Vector3(vx, 0f, vz); canAchieve = true; return true; } return false; }
- Finally, define the GetSteering function:
public override Steering GetSteering() { Steering steering = new Steering(); if (target == null) { CalculateTarget(); } if (!canAchieve) { return steering; } //Check if we've hit the jump point if (Mathf.Approximately((transform.position - target.transform.position).magnitude, 0f) && Mathf.Approximately((agent.velocity - target.GetComponent<Agent>().velocity).magnitude, 0f)) { // call a jump method based on the Projectile behaviour return steering; } return base.GetSteering(); }
How it works...
The algorithm takes into account the agent's velocity and calculates whether it can reach the landing pad or not. If it judges that the agent can, it tries to match the vertical velocity while seeking the landing pad's position.