// Update is called once per frame public override void Update() { Seperation seperation = new Seperation(); seperation.targets = obstacles; seperation.ai = this; SteeringOutput myMovement = seperation.GetSteering(); LookWhereGoing look = new LookWhereGoing(); look.ai = this; look.target = target; SteeringOutput mySteering = look.GetSteering(); if (mySteering != null) { //linearVelocity += steering.linear * Time.deltaTime; angularVelocity += mySteering.angular * Time.deltaTime; } linearVelocity += myMovement.linear * Time.deltaTime; transform.position += linearVelocity * Time.deltaTime; if (float.IsNaN(angularVelocity)) { angularVelocity = 0; } transform.eulerAngles += new Vector3(0, angularVelocity * Time.deltaTime, 0); }
void Update() { SteeringOutput steering; //Update position and rotation transform.position += linearVelocity * Time.deltaTime; Vector3 angularIncrement = new Vector3(0, angularVelocity * Time.deltaTime, 0); transform.eulerAngles += angularIncrement; switch (type) { case SteeringType.Pursue: steering = pursueAI.GetSteering(); break; case SteeringType.Evade: steering = evadeAI.GetSteering(); break; case SteeringType.FollowPath: steering = followAI.GetSteering(); break; case SteeringType.Seek: steering = seekAI.GetSteering(); break; case SteeringType.Flee: steering = fleeAI.GetSteering(); break; case SteeringType.Seperation: steering = seperationAI.GetSteering(); break; default: steering = seekAI.GetSteering(); break; } linearVelocity += steering.linear * Time.deltaTime; angularVelocity += steering.angular * Time.deltaTime; //Update kinematic reference with complex data it can't get by itself kinematic.GetData(steering); }
// Update is called once per frame void Update() { //Debug.Log(target); // Check distance to see if steering behavior should be applied to AI // float targetDist = (transform.position - target.transform.position).magnitude; // if (!DistanceActivation || targetDist >= activationRange) { } transform.position += linearVelocity * Time.deltaTime; // adding angular velocity to current transform rotation y component if (float.IsNaN(angularVelocity)) { angularVelocity = 0; } transform.eulerAngles += new Vector3(0, angularVelocity * Time.deltaTime, 0); // control to switch to proper steering behavior if (avoidObstacles) { ObstacleAvoidance avoid = new ObstacleAvoidance(); avoid.ai = this; SteeringOutput avoidForce = avoid.GetSteering(); if (avoidForce != null) { linearVelocity += avoidForce.linear; } } if (seperationObstacles.Length > 0) { Seperation seperation = new Seperation(); seperation.targets = seperationObstacles; seperation.ai = this; SteeringOutput seperateForce = seperation.GetSteering(); // check to see if steering is greater than zero and lock out control from other steering if (seperateForce.linear.magnitude > 0) { seperating = true; } else { seperating = false; } linearVelocity += seperateForce.linear * Time.deltaTime; } if (collisionAvoidance.Length > 0) { CollisionAvoidance avoidKinematic = new CollisionAvoidance(); avoidKinematic.ai = this; avoidKinematic.targets = collisionAvoidance; SteeringOutput avoidKinematicForce = avoidKinematic.GetSteering(); if (avoidKinematicForce != null) { linearVelocity += avoidKinematicForce.linear; } } switch (movementType) { case "seek": Seek mySeek = new Seek(); mySeek.ai = this; // if seek is false set seek property on class to false to activate flee mySeek.seek = true; mySeek.target = target; SteeringOutput steeringSeek = mySeek.GetSteering(); if (!seperating) { linearVelocity += steeringSeek.linear * Time.deltaTime; } if (linearVelocity.magnitude > maxSpeed) { linearVelocity.Normalize(); linearVelocity *= maxSpeed; } break; case "flee": Seek myFlee = new Seek(); myFlee.ai = this; // if seek is false set seek property on class to false to activate flee myFlee.seek = false; myFlee.target = target; SteeringOutput steeringFlee = myFlee.GetSteering(); if (!seperating) { linearVelocity += steeringFlee.linear * Time.deltaTime; } if (linearVelocity.magnitude > maxSpeed) { linearVelocity.Normalize(); linearVelocity *= maxSpeed; } break; case "arrive": Arrive myArrive = new Arrive(); myArrive.ai = this; myArrive.target = target; SteeringOutput steeringArrive = myArrive.GetSteering(); if (!seperating) { linearVelocity += steeringArrive.linear * Time.deltaTime; } break; case "pursue": Pursue myPursue = new Pursue(); myPursue.ai = this; myPursue.target = target; SteeringOutput steeringPursue = myPursue.GetSteering(); if (!seperating) { linearVelocity += steeringPursue.linear * Time.deltaTime; } if (linearVelocity.magnitude > maxSpeed) { linearVelocity.Normalize(); linearVelocity *= maxSpeed; } break; case "evade": Pursue myEvade = new Pursue(); myEvade.ai = this; myEvade.target = target; // This changes the seek flag in the parent Seek class of Pursue, sending it the flee vector instead myEvade.seek = false; SteeringOutput steeringEvade = myEvade.GetSteering(); if (!seperating) { linearVelocity += steeringEvade.linear * Time.deltaTime; } if (linearVelocity.magnitude > maxSpeed) { linearVelocity.Normalize(); linearVelocity *= maxSpeed; } break; default: // provide no input break; // If obstacles have been provided, return steering to seperate from them } switch (rotationType) { case "face": Face myFace = new Face(); myFace.ai = this; myFace.target = target; SteeringOutput steeringFace = myFace.GetSteering(); if (steeringFace != null) { // linearVelocity += steering.linear * Time.deltaTime; angularVelocity += steeringFace.angular * Time.deltaTime; } break; case "align": Align myAlign = new Align(); myAlign.ai = this; myAlign.target = target; SteeringOutput steeringAlign = myAlign.GetSteering(); if (steeringAlign != null) { //linearVelocity += steering.linear * Time.deltaTime; angularVelocity += steeringAlign.angular * Time.deltaTime; } break; case "look": LookWhereGoing myLook = new LookWhereGoing(); myLook.ai = this; myLook.target = target; SteeringOutput steeringLook = myLook.GetSteering(); if (steeringLook != null) { //linearVelocity += steering.linear * Time.deltaTime; angularVelocity += steeringLook.angular * Time.deltaTime; } break; default: //provide no input break; } }
void Update() { SteeringOutput movementSteering; //SteeringOutput lookSteering; //Update position and rotation transform.position += linearVelocity * Time.deltaTime; Vector3 angularIncrement = new Vector3(0, angularVelocity * Time.deltaTime, 0); transform.eulerAngles += angularIncrement; switch (moveType) { case SteeringType.Pursue: movementSteering = pursueAI.GetSteering(); break; case SteeringType.Evade: movementSteering = evadeAI.GetSteering(); break; case SteeringType.FollowPath: movementSteering = followAI.GetSteering(); break; case SteeringType.Seek: movementSteering = seekAI.GetSteering(); break; case SteeringType.Flee: movementSteering = fleeAI.GetSteering(); break; case SteeringType.Seperation: movementSteering = seperationAI.GetSteering(); break; case SteeringType.Arrive: movementSteering = arriveAI.GetSteering(); break; case SteeringType.CollisionAvoidance: movementSteering = avoidAI.GetSteering(); break; case SteeringType.ObstacleAvoidance: movementSteering = obstacleAI.GetSteering(); break; default: movementSteering = new SteeringOutput(); break; } if (movementSteering != null) { linearVelocity += movementSteering.linear * Time.deltaTime; //angularVelocity += movementSteering.angular * Time.deltaTime; } switch (lookType) { case LookType.None: break; case LookType.Align: lookSteering = alignAI.GetSteering(); break; case LookType.Face: lookSteering = faceAI.GetSteering(); break; case LookType.LookWhereGoing: lookSteering = lookAI.GetSteering(); break; default: lookSteering = alignAI.GetSteering(); break; } if (lookSteering != null) { angularVelocity += lookSteering.angular * Time.deltaTime; } //Update kinematic reference with complex data it can't get by itself kinematic.GetData(movementSteering); kinematic.GetData(lookSteering); }