// Update is called once per frame void Update() { MovementWithWeight mouseSeek = null; Debug.Log(this.blendedMovement.Movements.ToString()); if (Input.GetMouseButton(0)) { target.Position = new Vector3((((Input.mousePosition.x * X_WORLD_SIZE * 2) / Screen.width) - (X_WORLD_SIZE)), 0, (((Input.mousePosition.y * Z_WORLD_SIZE * 2) / Screen.height) - (Z_WORLD_SIZE))); var seekToPointMovement = new DynamicArrive() { Character = this.character.KinematicData, MaxAcceleration = MAX_ACCELERATION, Target = target }; mouseSeek = new MovementWithWeight(seekToPointMovement, 0.1f); this.blendedMovement.Movements.Add(mouseSeek); mouse = true; } UpdateMovingGameObject(); //the mouse variable can be for level 5 if (mouse) { this.blendedMovement.Movements.Remove(mouseSeek); mouse = false; } }
private List <DynamicMovement> InitBoidMovements(List <DynamicCharacter> characters, IEnumerable <GameObject> obstacles) { List <DynamicMovement> avoidObstacleMovement = new List <DynamicMovement>(); foreach (var obstacle in obstacles) { avoidObstacleMovement.Add(new DynamicAvoidObstacle(obstacle) { MaxAcceleration = MAX_ACCELERATION, AvoidMargin = AVOID_MARGIN, MaxLookAhead = MAX_LOOK_AHEAD, MovementDebugColor = Color.magenta }); } var cohesion = new DynamicCohesion() { Flock = this.Flock, MaxSpeed = MAX_SPEED, MaxAcceleration = MAX_ACCELERATION, StopRadius = 0.0F, SlowRadius = 6.0F, FlockRadius = COHESION_RADIUS, FanAngle = FAN_ANGLE, MovementDebugColor = Color.red }; var separation = new DynamicSeparation() { Flock = this.Flock, FlockRadius = SEPARATION_RADIUS, MaxAcceleration = MAX_ACCELERATION, SeparationFactor = SEPARATION_FACTOR, MovementDebugColor = Color.blue }; var matchVelocity = new DynamicFlockVelocityMatch() { Flock = this.Flock, MaxAcceleration = MAX_ACCELERATION, FlockRadius = COHESION_RADIUS, FanAngle = FAN_ANGLE, MovementDebugColor = Color.green }; var followMouse = new DynamicFollowMouse() { MaxSpeed = MAX_SPEED, MaxAcceleration = MAX_ACCELERATION, StopRadius = STOP_RADIUS, SlowRadius = SLOW_RADIUS, Arrived = new HashSet <KinematicData>(), MovementDebugColor = Color.gray }; FollowMouseMovement = new MovementWithWeight(followMouse, 0); var blended = new BlendedMovement(); blended.Movements.Add(new MovementWithWeight(cohesion, COHESION_WEIGHT)); blended.Movements.Add(new MovementWithWeight(separation, SEPARATION_WEIGHT)); blended.Movements.Add(new MovementWithWeight(matchVelocity, VELOCITY_MATCH_WEIGHT)); blended.Movements.Add(FollowMouseMovement); var allMovements = new List <DynamicMovement>(); allMovements.AddRange(avoidObstacleMovement); allMovements.Add(blended); return(allMovements); }
void Update() { Camera camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>(); Vector3 PointInWorld = new Vector3(); var buttonClicked = false; if (Input.GetKeyDown(KeyCode.Mouse0)) { var mousePos = Input.mousePosition; mousePos.z = CAMERA_Y; PointInWorld = camera.ScreenToWorldPoint(mousePos); buttonClicked = true; Debug.Log("world point: " + PointInWorld); } foreach (var character in this.Characters) { BlendedMovement movement = (BlendedMovement)character.Movement; MovementWithWeight fleeClick = movement.Movements.Find(x => x.Movement.Name == "Flee"); if (buttonClicked) { var distanceToPoint = (character.KinematicData.position - PointInWorld).magnitude; if (distanceToPoint < FLEE_RADIUS) { var dynamicFlee = new DynamicFlee() { Character = character.KinematicData, Target = new KinematicData(), MaxAcceleration = MAX_ACCELERATION }; if (fleeClick != null) { movement.Movements.Remove(fleeClick); } dynamicFlee.Target.position = PointInWorld; dynamicFlee.Target.position.y = character.KinematicData.position.y; movement.Movements.Add(new MovementWithWeight(dynamicFlee, 7)); } } else { var distanceToPoint = (character.KinematicData.position - PointInWorld).magnitude; if (distanceToPoint > FLEE_RADIUS) { if (fleeClick != null) { movement.Movements.Remove(fleeClick); } } } this.UpdateMovingGameObject(character); } }