Vector3 Seek(Vector3 targetPos) { Vector3 desiredVelocity; desiredVelocity = targetPos - transform.position; desiredVelocity.Normalize(); desiredVelocity *= maxSpeed; if (Params.drawDebugLines) { LineDrawer.DrawTarget(targetPos, Color.red); } return(desiredVelocity - velocity); }
public void Update() { float speed = 5.0f; float width = 500; float height = 500; if ((transform.position.x < -(width / 2)) || (transform.position.x > width / 2) || (transform.position.z < -(height / 2)) || (transform.position.z > height / 2) || (transform.position.y < 0) || (transform.position.y > 100)) { Destroy(gameObject); } transform.position += transform.forward * speed; LineDrawer.DrawLine(transform.position, transform.position + transform.forward * 10.0f, Color.red); }
public void Draw() { if (draw) { for (int i = 1; i < waypoints.Count(); i++) { LineDrawer.DrawLine(waypoints[i - 1], waypoints[i], Color.clear); } if (looped && (waypoints.Count() > 0)) { LineDrawer.DrawLine(waypoints[0], waypoints[waypoints.Count() - 1], Color.clear); } } }
public void Update() { if (drawGizmos) { LineDrawer.DrawSphere(transform.position, radius, 20, Color.yellow); } if (GetComponent <Boid>() != null) { foreach (GameObject o in boids) { o.GetComponent <Boid>().sphereCentre = transform.position; } } }
Vector3 Pursue() { Vector3 toTarget = target.transform.position - transform.position; float dist = toTarget.magnitude; float time = dist / maxSpeed; Vector3 targetPos = target.transform.position + (time * target.GetComponent <SteeringBehaviours>().velocity); if (Params.drawDebugLines) { LineDrawer.DrawLine(transform.position, targetPos, debugLineColour); } return(Seek(targetPos)); }
Vector3 Pursue() { Vector3 toTarget = pursuitTarget.transform.position - transform.position; float dist = toTarget.magnitude; float time = dist / maxSpeed; Vector3 targetPos = pursuitTarget.transform.position + (time * pursuitTarget.GetComponent <Boid>().velocity); if (drawGizmos) { LineDrawer.DrawTarget(targetPos, Color.red); LineDrawer.DrawLine(transform.position, targetPos, Color.cyan); } return(Seek(targetPos)); }
Vector3 Flee(Vector3 targetPos) { Vector3 desiredVelocity; desiredVelocity = transform.position - targetPos; if (desiredVelocity.magnitude > fleeRange) { return(Vector3.zero); } if (drawGizmos) { LineDrawer.DrawSphere(transform.position, fleeRange, 20, Color.yellow); } desiredVelocity.Normalize(); desiredVelocity *= maxSpeed; return(desiredVelocity - velocity); }
Vector3 Wander() { float jitterTimeSlice = Params.GetFloat("wander_jitter") * timeDelta; Vector3 toAdd = UnityEngine.Random.insideUnitSphere * jitterTimeSlice; wanderTargetPos += toAdd; wanderTargetPos.Normalize(); wanderTargetPos *= Params.GetFloat("wander_radius"); Vector3 localTarget = wanderTargetPos + Vector3.forward * Params.GetFloat("wander_distance"); Vector3 worldTarget = transform.TransformPoint(localTarget); if (Params.drawDebugLines) { LineDrawer.DrawTarget(worldTarget, Color.blue); } return(worldTarget - transform.position); }
Vector3 Wander() { float jitterTimeSlice = wanderJitter * timeDelta; Vector3 toAdd = UnityEngine.Random.insideUnitSphere * jitterTimeSlice; wanderTargetPos += toAdd; wanderTargetPos.Normalize(); wanderTargetPos *= wanderRadius; Vector3 localTarget = wanderTargetPos + Vector3.forward * wanderDistance; Vector3 worldTarget = transform.TransformPoint(localTarget); if (drawGizmos) { LineDrawer.DrawTarget(worldTarget, Color.blue); } return(worldTarget - transform.position); }
private int TagNeighboursSimple(float inRange) { tagged.Clear(); GameObject[] allBoids = GameObject.FindGameObjectsWithTag("boid"); foreach (GameObject boid in allBoids) { if (boid != gameObject) { if (drawNeighbours && Params.drawDebugLines) { LineDrawer.DrawLine(transform.position, boid.transform.position, Color.yellow); } if ((transform.position - boid.transform.position).magnitude < inRange) { tagged.Add(boid); } } } DrawNeighbours(Color.white); return(tagged.Count); }
// Use this for initialization void Start() { instance = this; }
Vector3 ObstacleAvoidance() { Vector3 force = Vector3.zero; makeFeelers(); List <GameObject> tagged = new List <GameObject>(); float minBoxLength = 50.0f; float boxLength = minBoxLength + ((velocity.magnitude / maxSpeed) * minBoxLength * 2.0f); if (float.IsNaN(boxLength)) { System.Console.WriteLine("NAN"); } GameObject[] obstacles = GameObject.FindGameObjectsWithTag("obstacle"); // Matt Bucklands Obstacle avoidance // First tag obstacles in range if (obstacles.Length == 0) { return(Vector3.zero); } foreach (GameObject obstacle in obstacles) { if (obstacle == null) { Debug.Log("Null object"); continue; } Vector3 toCentre = transform.position - obstacle.transform.position; float dist = toCentre.magnitude; if (dist < boxLength) { tagged.Add(obstacle); } } float distToClosestIP = float.MaxValue; GameObject closestIntersectingObstacle = null; Vector3 localPosOfClosestObstacle = Vector3.zero; Vector3 intersection = Vector3.zero; foreach (GameObject o in tagged) { Vector3 localPos = transform.InverseTransformPoint(o.transform.position); // If the local position has a positive Z value then it must lay // behind the agent. (in which case it can be ignored) if (localPos.z >= 0) { // If the distance from the x axis to the object's position is less // than its radius + half the width of the detection box then there // is a potential intersection. float obstacleRadius = o.transform.localScale.x / 2; float expandedRadius = GetRadius() + obstacleRadius; if ((Math.Abs(localPos.y) < expandedRadius) && (Math.Abs(localPos.x) < expandedRadius)) { // Now to do a ray/sphere intersection test. The center of the // Create a temp Entity to hold the sphere in local space Sphere tempSphere = new Sphere(expandedRadius, localPos); // Create a ray BGE.Geom.Ray ray = new BGE.Geom.Ray(); ray.pos = new Vector3(0, 0, 0); ray.look = Vector3.forward; // Find the point of intersection if (tempSphere.closestRayIntersects(ray, Vector3.zero, ref intersection) == false) { continue; } // Now see if its the closest, there may be other intersecting spheres float dist = intersection.magnitude; if (dist < distToClosestIP) { dist = distToClosestIP; closestIntersectingObstacle = o; localPosOfClosestObstacle = localPos; } } } } if (closestIntersectingObstacle != null) { // Now calculate the force float multiplier = 1.0f + (boxLength - localPosOfClosestObstacle.z) / boxLength; //calculate the lateral force float obstacleRadius = closestIntersectingObstacle.transform.localScale.x / 2; // closestIntersectingObstacle.GetComponent<Renderer>().bounds.extents.magnitude; float expandedRadius = GetRadius() + obstacleRadius; force.x = (expandedRadius - Math.Abs(localPosOfClosestObstacle.x)) * multiplier; force.y = (expandedRadius - Math.Abs(localPosOfClosestObstacle.y)) * multiplier; // Generate positive or negative direction so we steer around! // Not always in the same direction as in Matt Bucklands book if (localPosOfClosestObstacle.x > 0) { force.x = -force.x; } // If the obstacle is above, steer down if (localPosOfClosestObstacle.y > 0) { force.y = -force.y; } if (Params.drawDebugLines) { LineDrawer.DrawLine(transform.position, transform.position + transform.forward * boxLength, Color.grey); } //apply a braking force proportional to the obstacle's distance from //the vehicle. const float brakingWeight = 0.01f; force.z = (expandedRadius - localPosOfClosestObstacle.z) * brakingWeight; //finally, convert the steering vector from local to world space // Dont include position! force = transform.TransformDirection(force); } return(force); }
void Update() { float smoothRate; force = Calculate(); if (Params.drawForces) { Quaternion q = Quaternion.FromToRotation(Vector3.forward, force); LineDrawer.DrawArrowLine(transform.position, transform.position + force * 5.0f, Color.blue, q); } Utilities.checkNaN(force); Vector3 newAcceleration = force / mass; if (Params.drawVectors) { LineDrawer.DrawVectors(transform); } timeDelta = Time.deltaTime * Params.timeModifier; if (timeDelta > 0.0f) { smoothRate = Utilities.Clip(9.0f * timeDelta, 0.15f, 0.4f) / 2.0f; Utilities.BlendIntoAccumulator(smoothRate, newAcceleration, ref acceleration); } velocity += acceleration * timeDelta; float speed = velocity.magnitude; if (speed > maxSpeed) { velocity.Normalize(); velocity *= maxSpeed; } transform.position += velocity * timeDelta; // the length of this global-upward-pointing vector controls the vehicle's // tendency to right itself as it is rolled over from turning acceleration Vector3 globalUp = new Vector3(0, 0.2f, 0); // acceleration points toward the center of local path curvature, the // length determines how much the vehicle will roll while turning Vector3 accelUp = acceleration * 0.05f; // combined banking, sum of UP due to turning and global UP Vector3 bankUp = accelUp + globalUp; // blend bankUp into vehicle's UP basis vector smoothRate = timeDelta * 3.0f; Vector3 tempUp = transform.up; Utilities.BlendIntoAccumulator(smoothRate, bankUp, ref tempUp); if (speed > 0.0001f) { transform.forward = velocity; transform.forward.Normalize(); transform.LookAt(transform.position + transform.forward, tempUp); // Apply damping velocity *= 0.99f; } path.Draw(); if (Params.enforceNonPenetrationConstraint) { EnforceNonPenetrationConstraint(); } }
Vector3 SceneAvoidance() { Vector3 force = Vector3.zero; RaycastHit info; // Check the forward feeler and generate a up force Vector3 feeler = Vector3.forward; feeler = transform.TransformDirection(feeler); if (Physics.Raycast(transform.position, feeler, out info, sceneAvoidanceFeelerDepth)) { // Push me away in the direction of the up vector. // The deeper the penetration the bigger the force float depth = (info.point - feeler).magnitude; force = transform.up * depth; if (drawGizmos) { LineDrawer.DrawLine(transform.position, transform.position + feeler * sceneAvoidanceFeelerDepth, Color.red); } } // Check the bottom feeler and generate an upwards force feeler = Vector3.forward; feeler = Quaternion.AngleAxis(45, Vector3.right) * feeler; feeler = transform.TransformDirection(feeler); if (Physics.Raycast(transform.position, feeler, out info, sceneAvoidanceFeelerDepth)) { // Push me away in the direction of the up vector. // The deeper the penetration the bigger the force float depth = (info.point - feeler).magnitude; force += transform.up * depth; if (drawGizmos) { LineDrawer.DrawLine(transform.position, transform.position + feeler * sceneAvoidanceFeelerDepth, Color.red); } } // Check the top feeler and generate a downward force feeler = Vector3.forward; feeler = Quaternion.AngleAxis(-45, Vector3.right) * feeler; feeler = transform.TransformDirection(feeler); if (Physics.Raycast(transform.position, feeler, out info, sceneAvoidanceFeelerDepth)) { // Push me away in the direction of the up vector. // The deeper the penetration the bigger the force float depth = (info.point - feeler).magnitude; force -= transform.up * depth; if (drawGizmos) { LineDrawer.DrawLine(transform.position, transform.position + feeler * sceneAvoidanceFeelerDepth, Color.red); } } // Check the left feeler and generate a right force feeler = Vector3.forward; feeler = Quaternion.AngleAxis(45, Vector3.right) * feeler; feeler = transform.TransformDirection(feeler); if (Physics.Raycast(transform.position, feeler, out info, sceneAvoidanceFeelerDepth)) { // Push me away in the direction of the up vector. // The deeper the penetration the bigger the force float depth = (info.point - feeler).magnitude; force += transform.right * depth; if (drawGizmos) { LineDrawer.DrawLine(transform.position, transform.position + feeler * sceneAvoidanceFeelerDepth, Color.red); } } // Check the right feeler and generate a left force feeler = Vector3.forward; feeler = Quaternion.AngleAxis(-45, Vector3.right) * feeler; feeler = transform.TransformDirection(feeler); if (Physics.Raycast(transform.position, feeler, out info, sceneAvoidanceFeelerDepth)) { // Push me away in the direction of the up vector. // The deeper the penetration the bigger the force float depth = (info.point - feeler).magnitude; force -= transform.right * depth; if (drawGizmos) { LineDrawer.DrawLine(transform.position, transform.position + feeler * sceneAvoidanceFeelerDepth, Color.red); } } return(force); }