private void OnCollisionEnter(Collision col) { if (triggersRigidbodyAction && rigid != null) { if (bState == BumpState.Recovering) { this.transform.localRotation = Quaternion.identity; anim.SetTrigger("at rest"); bState = BumpState.Still; } else if (bState == BumpState.Still && col.transform.tag == "Player" && col.impulse.magnitude > bumpThreshold) { justCollided = true; collisionStrength = Mathf.Sqrt(col.impulse.magnitude) / Time.fixedDeltaTime; if (bumpSound != null) { audioFX.PlayOneShot(bumpSound); } } } if (triggersSimpleAction) { if (col.transform.tag == "Player" && col.impulse.magnitude > bumpThreshold) { simpleReactionIsActive = true; if (partsToBump.Count > 0) { foreach (BumpedPart bPart in partsToBump) { bPart.simpleRotationRefTime = Time.time; bPart.simpleTranslationRefTime = Time.time; if (bPart.rotationBackOrForthIsRandom && Random.Range(0, 2) == 0) { bPart.rotationDirection = bPart.rotationDirection * -1; } } } if (bumpSound != null) { audioFX.PlayOneShot(bumpSound); } } } }
// Start is called before the first frame update void Start() { if (triggersRigidbodyAction) { rigid = this.GetComponent <Rigidbody>(); } else { rigid = null; } coll = this.GetComponent <Collider>(); anim = this.GetComponent <Animator>(); audioFX = this.GetComponent <AudioSource>(); bState = BumpState.Still; justCollided = false; collisionStrength = 0; }
public void BumpIt() { if (triggersSimpleAction && rigid != null) { if (bState == BumpState.Recovering) { this.transform.localRotation = Quaternion.identity; anim.SetTrigger("at rest"); bState = BumpState.Still; } else if (bState == BumpState.Still) { justCollided = true; collisionStrength = Mathf.Sqrt(useBumpAmount) / Time.fixedDeltaTime; if (bumpSound != null) { audioFX.PlayOneShot(bumpSound); } } } if (triggersSimpleAction) { simpleReactionIsActive = true; if (partsToBump.Count > 0) { foreach (BumpedPart bPart in partsToBump) { bPart.simpleRotationRefTime = Time.time; bPart.simpleTranslationRefTime = Time.time; if (bPart.rotationBackOrForthIsRandom && Random.Range(0, 2) == 0) { bPart.rotationDirection = bPart.rotationDirection * -1; } } } if (bumpSound != null) { audioFX.PlayOneShot(bumpSound); } } }
private void ConstructFSM() { /* * //Get the list of points * pointList = GameObject.FindGameObjectsWithTag("WandarPoint"); * * Transform[] waypoints = new Transform[pointList.Length]; * int i = 0; * foreach(GameObject obj in pointList) * { * waypoints[i] = obj.transform; * i++; * } * * PatrolState patrol = new PatrolState(waypoints); * * patrol.AddTransition(Transition.SawPlayer, FSMStateID.Chasing); * patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead); */ /* ChaseState chase = new ChaseState(waypoints); * chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); * chase.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking); * chase.AddTransition(Transition.NoHealth, FSMStateID.Dead); * * AttackState attack = new AttackState(waypoints); * attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling); * attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing); * attack.AddTransition(Transition.NoHealth, FSMStateID.Dead); * */ // AddFSMState(patrol); /* AddFSMState(chase); * AddFSMState(attack); * AddFSMState(dead);*/ SwimState swim = new SwimState(); SawObstacleState sawObstacle = new SawObstacleState(); TurningAwayState turningAway = new TurningAwayState(); JumpState jump = new JumpState(); JumpingUnderWaterState jumpingUnderWater = new JumpingUnderWaterState(); JumpingAboveWaterState jumpingAboveWater = new JumpingAboveWaterState(); BumpState bump = new BumpState(); FallingState falling = new FallingState(); HitWaterState hitWater = new HitWaterState(); DeadState dead = new DeadState(); AddFSMState(swim); AddFSMState(sawObstacle); AddFSMState(turningAway); AddFSMState(jump); AddFSMState(jumpingUnderWater); AddFSMState(jumpingAboveWater); AddFSMState(falling); AddFSMState(hitWater); AddFSMState(bump); AddFSMState(dead); swim.AddTransition(Transition.Tapped, FSMStateID.Jump); swim.AddTransition(Transition.BumpedIntoSomething, FSMStateID.Bump); swim.AddTransition(Transition.SawObstacle, FSMStateID.SawObstacle); swim.AddTransition(Transition.SwimmingToTurning, FSMStateID.TurningAway); swim.AddTransition(Transition.GoneBelow, FSMStateID.Dead); sawObstacle.AddTransition(Transition.FoundClearDirection, FSMStateID.TurningAway); turningAway.AddTransition(Transition.TurnIsComplete, FSMStateID.Swimming); jump.AddTransition(Transition.HasJumped, FSMStateID.JumpingUnderWater); jumpingUnderWater.AddTransition(Transition.AboveWater, FSMStateID.JumpingAboveWater); jumpingUnderWater.AddTransition(Transition.ReturnToSwimming, FSMStateID.Swimming); jumpingAboveWater.AddTransition(Transition.ReachedApex, FSMStateID.Falling); falling.AddTransition(Transition.AtSurface, FSMStateID.HitWater); hitWater.AddTransition(Transition.UnderWater, FSMStateID.JumpingUnderWater); bump.AddTransition(Transition.HasBumped, FSMStateID.JumpingUnderWater); dead.AddTransition(Transition.NoHealth, FSMStateID.Dead); dead.AddTransition(Transition.Z, FSMStateID.Swimming); //falling.AddTransition(Transition.GoneBelow, FSMStateID.Dead); }
// Update is called once per frame void FixedUpdate() { //if (this.GetComponent<DocumentHandling>() != null) { // if (this.GetComponent<DocumentHandling>().buttonPressed && this.GetComponent<DocumentHandling>().documentBeingHeld) { // this.transform.localRotation = Quaternion.identity; // anim.SetTrigger("dropping"); // bState = BumpState.Recovering; // } //} if (triggersRigidbodyAction && rigid != null) { switch (bState) { case BumpState.Still: coll.enabled = true; if (justCollided) { coll.enabled = false; GotBumped(); //anim.SetTrigger("kicked up"); justCollided = false; bState = BumpState.BumpedAndReacting; } break; case BumpState.BumpedAndReacting: coll.enabled = false; //if (justCollided) { // GotBumped(); // anim.SetTrigger("kicked up"); // justCollided = false; //} if (rigid.velocity.y <= 0 || rigid.velocity.magnitude < 40) { this.transform.localRotation = Quaternion.identity; //anim.SetTrigger("dropping"); bState = BumpState.Recovering; } if (rigid.velocity.magnitude < 20) { this.transform.localRotation = Quaternion.identity; //anim.SetTrigger("at rest"); bState = BumpState.Still; } break; case BumpState.Recovering: coll.enabled = true; //if (justCollided) { // coll.enabled = false; // GotBumped(); // anim.SetTrigger("kicked up"); // justCollided = false; // bState = BumpState.BumpedAndReacting; //} if (rigid.velocity.magnitude < 15) { this.transform.localRotation = Quaternion.identity; //anim.SetTrigger("at rest"); bState = BumpState.Still; } break; } } else { switch (bumpMode) { case BumpType.SimpleTranslateNRotate: if (simpleReactionIsActive && partsToBump.Count > 0) { bool allPartsDoneReacting = true; foreach (BumpedPart bPart in partsToBump) { if (bPart.rotates && Time.time - bPart.simpleRotationRefTime < bPart.rotationDuration) { allPartsDoneReacting = false; if (bPart.rotatesAroundOwnOrigin) { bPart.part.Rotate(bPart.rotationDirection * (1 / ((1 + Time.time - bPart.simpleRotationRefTime) * bPart.rotationDecay)) * Time.deltaTime); } else { bPart.part.RotateAround(bPart.rotationOrigin, bPart.rotationDirection, bPart.rotationInterval * (1 / ((1 + Time.time - bPart.simpleRotationRefTime) * bPart.rotationDecay)) * Time.deltaTime); } } if (bPart.translates && Time.time - bPart.simpleTranslationRefTime < bPart.translationDuration) { bPart.part.Translate(bPart.translationDisplacementInterval * (1 / ((1 + Time.time - bPart.simpleTranslationRefTime) * bPart.translationDecay)) * Time.deltaTime); } } if (allPartsDoneReacting) { simpleReactionIsActive = false; } } break; case BumpType.TriggersAnimations: break; } } }