private void CheckForInput() { inputDirection = Input.GetAxisRaw("Horizontal"); //If not running and the run button is pressed, start running if (currentMovementState == movementState.normal) { if (Input.GetKeyDown(KeyCode.Space)) { StartDash(); } else if (inputDirection != 0) { playerRigidbody.velocity = new Vector2(walkSpeed * inputDirection, 0); } } // if (currentMovementState == movementState.running) { if (Input.GetKey(KeyCode.Space)) { playerRigidbody.velocity = new Vector2(runSpeed * inputDirection, 0); } else { currentMovementState = movementState.normal; } } }
void toggleCrawl() { if (move_state != movementState.CRAWL) { AudioController.audioPlayer.crouchSound(); body.transform.position = new Vector3(body.transform.position.x, .25f, body.transform.position.z); if (move_state == movementState.AGAINST_WALL) { // Move off of the wall before lying down to crawl body.transform.position += body.transform.forward * (this.transform.localScale.y / 2f); } body.transform.Rotate(new Vector3(90f, 0f, 0f)); move_state = movementState.CRAWL; } else if (!playerIsUnderObstacle()) { AudioController.audioPlayer.crouchSound(); body.transform.position = new Vector3(body.transform.position.x, 1f, body.transform.position.z); body.transform.Rotate(new Vector3(-90f, 0f, 0f)); move_state = movementState.RUN; } else { AudioController.audioPlayer.cantSound(); } }
// Use this for initialization protected void Start() { //Here be dragons um = GameObject.FindObjectOfType <UnitManager>(); em = GameObject.FindObjectOfType <EnemyManager>(); LADDERINDEX = TileSpecList.getTileSpecInt("Ladder"); currentMovement = new Vector2(0, 0); map = GameObject.FindObjectOfType <Map>(); currentState = movementState.IDLE; //Start the pathing coroutine StartCoroutine(getPath()); StartCoroutine(computeState()); cc = GetComponent <CharacterController>(); position = new Vector2(Mathf.RoundToInt(this.transform.position.x), Mathf.RoundToInt(this.transform.position.y)); selected = false; this.transform.localScale = new Vector3(size, size, size); this.right = Random.value > .5f; }
// Checks and returns the players state (Walking, Wading, Swimming, Diving) protected movementState StateCheck(Transform objectPosition, GameObject water) { float objectYPosition = objectPosition.transform.position.y; float waterYPosition = water.transform.position.y; movementState output = movementState.Walk; if (objectYPosition - 1.0f >= waterYPosition) { output = movementState.Walk; } else if (objectYPosition > waterYPosition) { output = movementState.Wade; } else if (objectYPosition + 1.0 >= waterYPosition) { output = movementState.Swim; } else if (objectYPosition + 1.0f < waterYPosition) { output = movementState.Dive; } return(output); }
private void Awake() { LockCursor(); isJump = false; currentPlayerState = movementState.Walk; previousPlayerState = movementState.Walk; }
private void rotate() { transformObj.rotation = Quaternion.RotateTowards(transformObj.rotation, targetRot, Time.deltaTime * rotateSpeed); if (input.horizontalInput == 0 && VectorEqual(targetRot.eulerAngles, transformObj.rotation.eulerAngles)) { stateMethod = idle; } }
public override void UpdatePlaying() { if (_movState == movementState.blink) { if (blinkTimer > 0.2f) { _movState = movementState.standing; blinkTimer = 0; } else { blinkTimer += Time.deltaTime; } } timeSinceGrounded += Time.deltaTime; if (timeSinceGrounded > 0.1f) { grounded = false; surfaceNormal = Vector3.up; } else if (_movState != movementState.blink) { grounded = true; } transform.rotation = Quaternion.Euler(0, newLook.y, 0); if (PlayerCore._instance.playerState != PlayerCore.inputState.ragdoll) { //if (!PlayerCore._instance.peeking) { head.position = new Vector3(transform.position.x, transform.position.y + eyeHeight, transform.position.z); head.rotation = Quaternion.Euler(newLook); //} } Vector3 flatMag = move.velocity; flatMag.y = 0; if (_movState == movementState.blink) { _camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, 179, 0.5f); } else { _camera.fieldOfView = Mathf.Lerp(_camera.fieldOfView, 80, 0.2f); } if (AudioManager._instance != null && move.velocity.magnitude > 7.7f) { AudioManager._instance.FastMusic((move.velocity.magnitude - 7.6f) / 12); } else { AudioManager._instance.FastMusic(0); } }
void Start() { if (thisRB == null) { thisRB = GetComponent <Rigidbody>(); } thisMovementState = movementState.FreeMovement; startHeight = thisRB.position.y; }
// Update is called once per frame void Update() { if (this.transform.position.x <= pos1.x && curr == movementState.forward) { curr = movementState.back; enemyBod.velocity = new Vector3 (speed, 0f , 0f); } else if (this.transform.position.x >= camW/2 && curr == movementState.back) { Destroy(this.gameObject); } }
void StartDash() { inputDirection = Input.GetAxisRaw("Horizontal"); if (inputDirection == 0) { inputDirection = 1; } playerRigidbody.velocity = new Vector2(runSpeed * inputDirection, 0); currentMovementState = movementState.running; }
public void checkIfPushed() { if(movScript.state == CharacterMovement.states.pushingLeft){ myState = movementState.pushedLeft; } else if(movScript.state == CharacterMovement.states.pushingRight){ myState = movementState.pushedRight; } }
// Update is called once per frame void Update() { if (this.transform.position.x <= pos1.x && curr == movementState.forward) { curr = movementState.back; enemyBod.velocity = new Vector3(speed, 0f, 0f); } else if (this.transform.position.x >= camW / 2 && curr == movementState.back) { Destroy(this.gameObject); } }
void ladderTile(IVector2 t) { if (!canLadder) { return; } currentState = movementState.CLIMBING; TileSpec ts = map.getForeground(t); if (!ts.solid && ts != TileSpecList.getTileSpec(LADDERINDEX)) { map.setTile(t, (byte)LADDERINDEX, (byte)1); } }
private void idle() { if (input.verticalInput != 0) { if (!checkCollision(input.verticalInput)) { stateMethod = move; targetPos = transformObj.position + transformObj.forward * input.verticalInput; } else { stateMethod = bump; targetPos = transformObj.position; } } else if (input.horizontalInput != 0) { targetRot.eulerAngles = new Vector3 (transformObj.rotation.eulerAngles.x, transformObj.rotation.eulerAngles.y + (90 * input.horizontalInput), transformObj.rotation.eulerAngles.z); stateMethod = rotate; } }
private void Search() { if (currentState == movementState.FOLLOW) { if (transform.localScale.x == 0.5f) { currentState = movementState.RIGHT; } if (transform.localScale.x == -0.5f) { currentState = movementState.LEFT; } } }
//****************************************************************************// void walkcase(IVector2 currentpos, IVector2 dest, ParentedNode node, Vector2 v) { if ((v.y > .25f && Mathf.Abs(v.x) < .1f) || (map.getForeground(currentpos) == TileSpecList.getTileSpec(LADDERINDEX) && canLadder)) { laddercase(currentpos, dest, node, v); return; } currentState = movementState.WALKING; if (v.y > .25f && (Mathf.Abs(v.x) > .1f && cc.velocity.x == 0)) // || (lastPosition - currentpos).magnitude == 0){ { Debug.Log("Jumping"); jump(); currentState = movementState.JUMPING; } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.P) && state == movementState.moving) { state = movementState.stopping; } if (Input.GetKeyDown(KeyCode.S)) { state = movementState.starting; } switch (state) { case movementState.moving: positionDelta = velocityMoving * direction * Time.deltaTime; break; case movementState.starting: positionDelta += (startingAcceleration * direction * Time.deltaTime); if (Mathf.Abs(positionDelta) >= (velocityMoving)) { state = movementState.moving; } break; case movementState.stopping: positionDelta -= (stoppingAcceleration * direction * Time.deltaTime); if (direction > 0f && positionDelta < 0f) { state = movementState.starting; direction = direction * -1f; } if (direction < 0f && positionDelta > 0f) { state = movementState.starting; direction = direction * -1f; } break; } transform.position = new Vector3(transform.position.x + positionDelta, transform.position.y, transform.position.z); }
void Update() { transform.RotateAround(transform.position, Vector3.forward, 400 * Time.deltaTime); var cos = Mathf.Cos(Mathf.PI / 4); var sin = Mathf.Sin(Mathf.PI / 4); if (this.transform.position.x <= -camW / 8 && curr == movementState.forward) { //if (this.transform.position.x <= shiprigidbody.transform.position.x && curr == movementState.forward) { curr = movementState.back; //enemyBod.velocity = new Vector3 (speed, 0f , 0f); if (startfrom == occurplace.up) { sin = -sin; } enemyBod.velocity = new Vector3(speed * cos, speed * sin, 0f); } else if (curr == movementState.back) { //if (this.transform.position.y <= shiprigidbody.transform.position.y && startfrom == occurplace.up) { if (this.transform.position.y <= 0f && startfrom == occurplace.up) { curr = movementState.rotate; enemyBod.velocity = new Vector3(enemyBod.velocity.x * cos - enemyBod.velocity.y * sin, enemyBod.velocity.x * sin + enemyBod.velocity.y * cos, 0f); } //else if (this.transform.position.y >= shiprigidbody.transform.position.y && startfrom == occurplace.down) { else if (this.transform.position.y >= 0f && startfrom == occurplace.down) { sin = -sin; curr = movementState.rotate; enemyBod.velocity = new Vector3(enemyBod.velocity.x * cos - enemyBod.velocity.y * sin, enemyBod.velocity.x * sin + enemyBod.velocity.y * cos, 0f); } } else if (this.transform.position.x >= camW / 2 && curr != movementState.forward) { Destroy(this.gameObject); } else if (this.gameObject.transform.position.y - this.gameObject.transform.lossyScale.y / 2 <= -camH / 2 || this.gameObject.transform.position.y + this.gameObject.transform.lossyScale.y / 2 >= camH / 2) { Destroy(this.gameObject); } }
void updateAlongWall() { if (movingInLockedDirection()) { if (!movingDiagonal()) { move_state = movementState.AGAINST_WALL; body.transform.rotation = Quaternion.LookRotation(locked_direction * -1); moveBackAgainstWall(); } zeroMovementInLockedDirection(); } else if (movingOppositeOfLockedDirection()) { locked_direction = Vector3.zero; move_state = movementState.RUN; } }
void updateWallMovement() { if (nextToObstacle() || hit_corner) { hit_corner = false; if (move_state == movementState.ALONG_WALL) { updateAlongWall(); } else if (move_state == movementState.AGAINST_WALL) { updateAgainstWall(); } } else if (move_state != movementState.CRAWL && move_state != movementState.GRABBING) { move_state = movementState.RUN; locked_direction = Vector3.zero; } }
private void Update() { playerInputs = ReturnPlayerInputs(); grounded = Grounded(transform.position, capsuleCol); initialPosition = transform.position; isSteep = IsSteep(SteepCheck(bodyObject), maxSlopeAngle); currentPlayerState = StateCheck(bodyObject, water); if (previousPlayerState != currentPlayerState) { Debug.Log(currentPlayerState); } previousPlayerState = currentPlayerState; CameraRotation(); SimpleMove(); PlayerPhysics(); FinalMove(); PlayerCollision(); }
void updateAgainstWall() { if (movingOppositeOfLockedDirection()) { locked_direction = Vector3.zero; move_state = movementState.RUN; return; } if (!movingInLockedDirection()) { this.transform.position += this.transform.forward * 0.5f; locked_direction = Vector3.zero; move_state = movementState.RUN; } else { zeroMovementInLockedDirection(); } }
//Handles the character digging. public void handleDigging(Vector2 dest, Vector2 v) { if (digging) { this.currentState = movementState.DIGGING; if (v.y < -.25) { jump(.75f); emitParticles(); } //If it is done digging if ((digTimer -= Time.fixedDeltaTime) <= 0) { mine(dest); } } //Detect if it needs to start digging else if (map.isForegroundSolid(dest)) { digTimer = digTime; digging = true; } }
void resolveGrab() { if (move_state == movementState.RUN) { body.velocity = Vector3.zero; RaycastHit hit_info; Ray facing = new Ray(transform.position - transform.forward, transform.forward); Debug.DrawRay(transform.position - transform.forward * 2, transform.forward, Color.blue, 2f); Debug.DrawRay(transform.position, transform.right, Color.green, 4f); if (Physics.SphereCast(facing, 1.1f, out hit_info, 2f, enemy_layer)) { victim = hit_info.rigidbody.gameObject; if (victim.GetComponent <Enemy>().current_state != EnemyState.KO && victim.GetComponent <Enemy>().current_state != EnemyState.BEING_FLIPPED && victim.GetComponent <Enemy>().current_state != EnemyState.DEAD) { if (moveInput() && !body.isKinematic && !flip_lock) { throwEnemy(); } else { move_state = movementState.GRABBING; victim.GetComponent <Enemy>().getGrabbed(); } } } } else if (move_state == movementState.GRABBING) { if (victim.GetComponent <Enemy>().current_state != EnemyState.GRABBED) { move_state = movementState.RUN; } } }
void OnCollisionEnter(Collision coll) { Vector3 pos; if (coll.gameObject.tag == "Obstacle" && move_state == movementState.RUN) { foreach (ContactPoint contact in coll.contacts) { if (contact.normal.x != 0 || contact.normal.z != 0) { locked_direction = coll.contacts[0].normal * -1; locked_direction.y = 0; break; } } if (locked_direction.x != 0 && locked_direction.z != 0) { hit_corner = true; locked_direction.z = 0; pos = this.transform.position; pos.x += locked_direction.x * -0.5f; pos.z += body.velocity.normalized.z * -0.5f; this.transform.position = pos; } if (movingDiagonal()) { move_state = movementState.ALONG_WALL; } else { move_state = movementState.AGAINST_WALL; body.transform.rotation = Quaternion.LookRotation(coll.contacts[0].normal); } } }
// Use this for initialization protected void Start () { //Here be dragons um = GameObject.FindObjectOfType<UnitManager>(); em = GameObject.FindObjectOfType<EnemyManager>(); LADDERINDEX = TileSpecList.getTileSpecInt("Ladder"); currentMovement = new Vector2(0,0); map = GameObject.FindObjectOfType<Map>(); currentState = movementState.IDLE; //Start the pathing coroutine StartCoroutine(getPath()); StartCoroutine(computeState()); cc = GetComponent<CharacterController>(); position = new Vector2(Mathf.RoundToInt(this.transform.position.x),Mathf.RoundToInt(this.transform.position.y)); selected = false; this.transform.localScale = new Vector3(size,size,size); this.right = Random.value>.5f; }
void Update() { transform.RotateAround(transform.position, Vector3.forward, 400* Time.deltaTime); var cos = Mathf.Cos (Mathf.PI /4); var sin = Mathf.Sin (Mathf.PI /4); if (this.transform.position.x <= - camW /8 && curr == movementState.forward) { //if (this.transform.position.x <= shiprigidbody.transform.position.x && curr == movementState.forward) { curr = movementState.back; //enemyBod.velocity = new Vector3 (speed, 0f , 0f); if (startfrom == occurplace.up) { sin = -sin; } enemyBod.velocity = new Vector3 (speed * cos, speed * sin, 0f); } else if (curr == movementState.back) { //if (this.transform.position.y <= shiprigidbody.transform.position.y && startfrom == occurplace.up) { if (this.transform.position.y <= 0f && startfrom == occurplace.up) { curr = movementState.rotate; enemyBod.velocity = new Vector3 (enemyBod.velocity.x * cos - enemyBod.velocity.y * sin, enemyBod.velocity.x * sin + enemyBod.velocity.y * cos, 0f); } //else if (this.transform.position.y >= shiprigidbody.transform.position.y && startfrom == occurplace.down) { else if (this.transform.position.y >= 0f && startfrom == occurplace.down) { sin = -sin; curr = movementState.rotate; enemyBod.velocity = new Vector3 (enemyBod.velocity.x * cos - enemyBod.velocity.y * sin, enemyBod.velocity.x * sin + enemyBod.velocity.y * cos, 0f); } } else if (this.transform.position.x >= camW / 2 && curr != movementState.forward) { Destroy (this.gameObject); } else if (this.gameObject.transform.position.y - this.gameObject.transform.lossyScale.y / 2 <= -camH / 2 || this.gameObject.transform.position.y + this.gameObject.transform.lossyScale.y / 2 >= camH / 2) { Destroy (this.gameObject); } }
protected IEnumerator computeState(){ int counter = 0; while(true){ if(pPhysics.climbing){ currentState = movementState.CLIMBING; } switch(currentState){ case movementState.IDLE: animater.animationID = 0; if(moveAmount.x != 0){ currentState = movementState.WALKING; } if(!pPhysics.onGround && moveAmount.y != 0){ currentState = movementState.JUMPING; } break; case movementState.JUMPING: animater.animationID = 2; if(pPhysics.onGround){ currentState = movementState.WALKING; } break; case movementState.WALKING: animater.animationID = 1; if(!pPhysics.onGround && moveAmount.y != 0){ currentState = movementState.JUMPING; } else{ if(moveAmount.x == 0){ currentState = movementState.IDLE; } } break; case movementState.CLIMBING: animater.animationID = 3; if(!pPhysics.climbing){ currentState = movementState.WALKING; } break; } if(moveAmount.x < 0){ right = false; } if(moveAmount.x > 0){ right = true; } yield return null; } }
void Update() { switch (thisMovementState) { case movementState.FreeMovement: //Do rotation float RSInput = Input.GetAxisRaw("RightStickX"); if (RSInput != 0.0f) { Vector3 eulerAngleVelocity = new Vector3(0.0f, RSInput * maxRotationSpeed, 0.0f); Quaternion deltaRotation = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime); thisRB.MoveRotation(thisRB.rotation * deltaRotation); } //Do movement if (UseTriggerDownMovement) { float triggerInput = 0.0f; float triggerInputConverted = 0.0f; if (UseController) { triggerInput = Input.GetAxisRaw("R2"); triggerInputConverted = (triggerInput + 1.0f) / 2.0f; //ps4 trigger goes from -1.0 -> 1.0 >.< } else { triggerInput = triggerInputConverted = Input.GetAxis("Down"); } bool goingDown = triggerInput - prevTriggerInput >= 0.0f; prevTriggerInput = triggerInput; RaycastHit hit; Vector3 rayStartPoint = new Vector3(thisRB.position.x, startHeight, thisRB.position.z); if (Physics.Raycast(rayStartPoint, -Vector3.up, out hit)) { downMoveGrappleLength = hit.distance - downMoveLengthOffset; if (downMoveGrappleLength > downMoveMaxLength) { downMoveGrappleLength = downMoveMaxLength; } } float verticalTargetPos = startHeight - (triggerInputConverted * downMoveGrappleLength); if (goingDown && verticalTargetPos <= startHeight - downMoveGrappleLength + vertMoveEaseLength || ((!goingDown || triggerInputConverted == 0.0f) && verticalTargetPos >= startHeight - vertMoveEaseLength)) { float vertVel = verticalTargetPos - thisRB.position.y; vertVel *= vertMoveEaseMultiplier; float newPosY = thisRB.position.y + vertVel; thisRB.MovePosition(new Vector3(thisRB.position.x, newPosY, thisRB.position.z)); } else { thisRB.MovePosition(new Vector3(thisRB.position.x, verticalTargetPos, thisRB.position.z)); } } Vector3 movementInput = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical")); movementInput.Normalize(); movementInput *= maxMoveSpeed * Time.deltaTime; thisRB.MovePosition(thisRB.position + movementInput); //Check grapple button if (!UseTriggerDownMovement && Input.GetButtonDown("X")) { thisMovementState = movementState.GrapplingDown; grappleReturnDestination = thisRB.position; grappleTargetDestination = thisRB.position + new Vector3(0.0f, -downMoveGrappleLength); } break; case movementState.GrapplingDown: //Do movement curVelocity = new Vector3(0.0f, -maxDownMoveSpeed); newPosition = thisRB.position + (curVelocity * Time.deltaTime); if (newPosition.y <= grappleTargetDestination.y) { thisRB.MovePosition(grappleTargetDestination); thisMovementState = movementState.ReturningUp; } else { thisRB.MovePosition(newPosition); } break; case movementState.ReturningUp: //Do movement curVelocity = new Vector3(0.0f, maxUpMoveSpeed); newPosition = thisRB.position + (curVelocity * Time.deltaTime); if (newPosition.y >= grappleReturnDestination.y) { thisRB.MovePosition(grappleReturnDestination); thisMovementState = movementState.FreeMovement; } else { thisRB.MovePosition(newPosition); } break; default: Debug.LogError("Grijper MovementState not set correctly!"); break; } }
void toggleCrawl() { if (move_state != movementState.CRAWL) { AudioController.audioPlayer.crouchSound(); body.transform.position = new Vector3(body.transform.position.x, .25f, body.transform.position.z); if (move_state == movementState.AGAINST_WALL) { // Move off of the wall before lying down to crawl body.transform.position += body.transform.forward * (this.transform.localScale.y / 2f); } body.transform.Rotate(new Vector3(90f, 0f, 0f)); move_state = movementState.CRAWL; } else if (!playerIsUnderObstacle()) { AudioController.audioPlayer.crouchSound(); body.transform.position = new Vector3(body.transform.position.x, 1f, body.transform.position.z); body.transform.Rotate(new Vector3(-90f, 0f, 0f)); move_state = movementState.RUN; } else AudioController.audioPlayer.cantSound(); }
private void FixedUpdate() { switch (movState) { case movementState.Walking: ResetHeight(); ResetSpeed(); sliding = false; crouching = false; break; case movementState.Sprinting: Sprint(); break; case movementState.Crouching: Crouch(); sliding = false; break; case movementState.Slide: Slide(); break; } #region Controls Inputs if (Input.GetButton("Horizontal") || Input.GetButton("Vertical")) { moveNextUpdate = true; } if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0) { rotateNextUpdate = true; } if (Input.GetButton("Jump")) { JumpNextUpdate = true; } if (Input.anyKey && isDead) { Restart(); } if (Input.GetButton("Crouch")) { if (movState == movementState.Sprinting || sliding) { movState = movementState.Slide; } else if (movState == movementState.Walking || crouching) { movState = movementState.Crouching; } } else if (Input.GetButton("Sprint")) { movState = movementState.Sprinting; } else { movState = movementState.Walking; } #endregion #region Input Bools if (moveNextUpdate) { Move(); moveNextUpdate = false; } if (rotateNextUpdate) { Rotate(); rotateNextUpdate = false; } if (JumpNextUpdate) { Jump(); JumpNextUpdate = false; } #endregion if (sliding) { slideSpeed = slideSpeed - 0.2f; slideSpeed = Mathf.Clamp(slideSpeed, 0, maxSlideSpeed); if (slideSpeed == 0) { movState = movementState.Crouching; } } }
void OnTriggerExit(Collider col) { if(col.gameObject.tag == "Player"){ myState = movementState.neutral; } }
public void flipPlayer (float dir) { stateMethod = rotate; targetRot.eulerAngles = new Vector3 (transformObj.rotation.eulerAngles.x, transformObj.rotation.eulerAngles.y + (90), transformObj.rotation.eulerAngles.z); }
void resolveGrab() { if (move_state == movementState.RUN) { body.velocity = Vector3.zero; RaycastHit hit_info; Ray facing = new Ray(transform.position - transform.forward, transform.forward); Debug.DrawRay(transform.position - transform.forward * 2, transform.forward, Color.blue, 2f); Debug.DrawRay(transform.position, transform.right, Color.green, 4f); if (Physics.SphereCast(facing, 1.1f, out hit_info, 2f, enemy_layer)) { victim = hit_info.rigidbody.gameObject; if (victim.GetComponent<Enemy>().current_state != EnemyState.KO && victim.GetComponent<Enemy>().current_state != EnemyState.BEING_FLIPPED && victim.GetComponent<Enemy>().current_state != EnemyState.DEAD) { if (moveInput() && !body.isKinematic && !flip_lock) throwEnemy(); else { move_state = movementState.GRABBING; victim.GetComponent<Enemy>().getGrabbed(); } } } } else if (move_state == movementState.GRABBING) { if (victim.GetComponent<Enemy>().current_state != EnemyState.GRABBED) { move_state = movementState.RUN; } } }
private void checkGround() { RaycastHit hit; if (Physics.Raycast (transformObj.position, -transformObj.up, out hit, 1.0f)) { hit.collider.gameObject.SendMessage ("OnContact", transformObj.gameObject); stateMethod = idle; } else { stateMethod = idle; } }
// Update is called once per frame void Update () { //grab the forwards vector for when the body is facedown (crawl state) Vector3 crawlForwardVector = findForwardCrawlVector(); //Debug.DrawRay(body.transform.position, crawlForwardVector * 5f, Color.green); if (Input.GetKeyDown(KeyCode.I)) { if(!bigBossMode) { bigBossMode = true; print("Invincibility Mode ACTIVATED!"); } else { bigBossMode = false; print("Invincibility Mode DE-ACTIVATED!"); } } if(currState == movementState.run || (inCrouchMode && currState == movementState.crawl) || currState == movementState.sneak) { if (Input.GetKeyDown(KeyCode.W)) { print("Triangle Button: FPV Cam"); FPVModeControl = true; CameraController.S.SwitchCameraTo(CameraType.fpv); } if (Input.GetKeyUp(KeyCode.W)) { FPVModeControl = false; CameraController.S.SwitchCameraTo(CameraType.overhead); } } Vector3 vel = Vector3.zero; if (Input.GetKey (KeyCode.UpArrow)) { vel.z += 1; } if (Input.GetKey (KeyCode.DownArrow)) { vel.z -= 1; } if (Input.GetKey (KeyCode.LeftArrow)) { vel.x -= 1; } if (Input.GetKey (KeyCode.RightArrow)) { vel.x += 1; } if(HandController.S.isFighting || HandController.S.isFighting || FPVModeControl) { vel = Vector3.zero; } // begin of FPV mode control GameObject fpvcamera = CameraController.S.cameras[(int)CameraType.fpv]; if (FPVModeControl) { //controls for FPV mode if(Input.GetKey(KeyCode.UpArrow)) { Quaternion rot = Quaternion.Euler(-45f, 0f, 0f); fpvcamera.transform.localRotation = Quaternion.Slerp(fpvcamera.transform.localRotation, rot, .05f); } if (Input.GetKey(KeyCode.DownArrow)) { Quaternion rot = Quaternion.Euler(45f, 0f, 0f); fpvcamera.transform.localRotation = Quaternion.Slerp(fpvcamera.transform.localRotation, rot, .05f); } if (Input.GetKey(KeyCode.LeftArrow)) { lastFPVForward = (new Vector3(0f, -FPVRotationSpeedDeg, 0f)); } else if (Input.GetKey(KeyCode.RightArrow)) { lastFPVForward = (new Vector3(0f, FPVRotationSpeedDeg, 0f)); } else { if (timeTillTurnUpdate <= 0) { lastFPVForward *= FPVRotationDecel; } } if (timeTillTurnUpdate > 0) { timeTillTurnUpdate -= Time.deltaTime; } else { timeTillTurnUpdate = 1f / 60f; body.transform.Rotate(lastFPVForward); } } if (fpvcamera.transform.localRotation != defaultFPVCameraAngle && !Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.UpArrow)) { fpvcamera.transform.localRotation = Quaternion.Slerp(fpvcamera.transform.localRotation, defaultFPVCameraAngle, .05f); } //perform wall stick sneak movements if (currState == movementState.sneak) { if (Vector3.Dot (vel, gameObject.transform.forward) > 0 && Vector3.Angle (vel, gameObject.transform.forward) < 90) { if (inCrouchMode) { currState = movementState.crawl; gameObject.transform.position += vel.normalized * sneakCrouchWallBuffer; } else { currState = movementState.run; } collided = collideToStick; collided2 = collideToStick * 2; } else if (!doubleRaycast (gameObject.transform.position, -body.transform.forward, (gameObject.transform.lossyScale.z / 2) + .2f)) { if (inCrouchMode) { currState = movementState.crawl; gameObject.transform.position += vel.normalized * sneakCrouchWallBuffer; } else { currState = movementState.run; } collided = collideToStick; collided2 = collideToStick * 2; } else if (inCrouchMode) { vel = Vector3.zero; } else if (moveLock == movementLock.LR) { vel.x = 0; } else if (moveLock == movementLock.FB) { vel.z = 0; } // SNEAK CAM CODE START // If sneaking, then check to see if you're at an edge Vector3 leftPosition, rightPosition; bool leftEdge, rightEdge; RaycastHit leftHit, rightHit; rightPosition = body.transform.position + body.transform.right + (body.transform.forward * -1); leftPosition = body.transform.position + (-1 * body.transform.right) + (body.transform.forward * -1); rightPosition.y += 3f; leftPosition.y += 3f; // Debug rays Debug.DrawRay (rightPosition, new Vector3(0f,-4f,0f)); Debug.DrawRay (leftPosition, new Vector3(0f,-4f, 0f)); // change camera position to sneak cam position // If either of the raycasts don't hit anything, then player is at an edge leftEdge = Physics.Raycast (leftPosition, new Vector3(0f,-1f,0f), out leftHit, 4f); rightEdge = Physics.Raycast (rightPosition, new Vector3(0f,-1f,0f), out rightHit, 4f); if (leftEdge) { if (leftHit.collider.tag != "Floor") leftEdge = false; } if (rightEdge) { if (rightHit.collider.tag != "Floor") rightEdge = false; } if (leftEdge && rightEdge) { CameraController.S.GetComponent<CameraFollow> ().activateSneakCam (3, leftPosition+rightPosition); } else if (leftEdge) { CameraController.S.GetComponent<CameraFollow> ().activateSneakCam (0, leftPosition); } else if (rightEdge) { CameraController.S.GetComponent<CameraFollow> ().activateSneakCam (1, rightPosition); } // Otherwise, you're not at an edge else { // Change camera position back to normal CameraController.S.GetComponent<CameraFollow> ().deactivateSneakCam (); } // SNEAK CAM CODE END } // Also make sure that cam is deactivated to default once you leave sneak else { CameraController.S.GetComponent<CameraFollow> ().deactivateSneakCam (); } // Save the last known button press direction if (vel != Vector3.zero) lastVel = vel; //CROUCH logic: transform snake back to normal if he moves if(inCrouchMode && vel != Vector3.zero) { body.transform.localScale = defaultPlayerSize; //check for nearby walls so we don't clip inside them Ray frontray = new Ray(body.transform.position, body.transform.forward); //Ray buttray = new Ray(body.transform.position, -body.transform.forward); if (Physics.Raycast(frontray, body.transform.lossyScale.y / 2f, wallLayerMask)) { Vector3 safePos = gameObject.transform.position; safePos += -body.transform.forward.normalized * ((body.transform.lossyScale.y / 2f) + .1f); safePos.y = .2f; body.transform.position = safePos; } else { body.transform.position = new Vector3(body.transform.position.x, 0.2f, body.transform.position.z); } body.transform.rotation = Quaternion.LookRotation(vel.normalized); body.transform.Rotate(new Vector3(90f, 0f, 0f)); inCrouchMode = false; } //CRAWL box collider, shrink down the box collider when in crawl mode if(currState == movementState.crawl && !inCrouchMode) { boxcollider.size = new Vector3(boxcollider.size.x, 1f, boxcollider.size.z); boxcollider.center = new Vector3(boxcollider.center.x, .3f, boxcollider.center.z); } else { boxcollider.size = defaultBoxColliderSize; boxcollider.center = new Vector3(boxcollider.center.x, .15f, boxcollider.center.z); } //MOVEMENT of the Character // Speed and direction of movement depends on movementState if (inFPVModeCrawlTransition > 0 && currState == movementState.crawl) { inFPVModeCrawlTransition -= Time.deltaTime; body.velocity = lastForwardCrawlVector; } else if(HandController.S.isFighting || HandController.S.isGrabbing) { //don't set velocity here } else if (currState == movementState.run) { body.velocity = vel.normalized * speed; } else if(!FPVModeCrawlControl) { body.velocity = vel.normalized * slowSpeed; } else //first person crawl mode uses a different set of controls if(FPVModeCrawlControl) { if (Input.GetKey(KeyCode.UpArrow)) { body.velocity = crawlForwardVector * slowSpeed; } if (Input.GetKey(KeyCode.DownArrow)) { body.velocity = -crawlForwardVector * slowSpeed; } if (Input.GetKey(KeyCode.LeftArrow)) { lastCrawlTurnVector = (new Vector3(0f, 0f, FPVRotationSpeedDeg)); } else if (Input.GetKey(KeyCode.RightArrow)) { lastCrawlTurnVector = (new Vector3(0f, 0f, -FPVRotationSpeedDeg)); } else { if(timeTillTurnUpdate <= 0) { lastCrawlTurnVector *= FPVRotationDecel; } } if (timeTillTurnUpdate > 0) { timeTillTurnUpdate -= Time.deltaTime; } else { timeTillTurnUpdate = 1f / 60f; body.transform.Rotate(lastCrawlTurnVector); } }// end of FPV mode crawl control //ROTATIONS, face character towards the vector of movement: //set our foward direction if in run state (run rotation) if (currState == movementState.run && !FPVModeControl) { if (Vector3.Angle(body.transform.forward, lastVel.normalized) > 45f) { body.velocity = Vector3.zero; } // Lerp for smooth rotation body.transform.rotation = Quaternion.LookRotation(Vector3.Slerp(body.transform.forward, lastVel.normalized, rotationSpeed)); //body.transform.rotation = Quaternion.LookRotation(lastVel.normalized); } else //set look direction in crawl mode, unless snake is backing up, or in FPV Crawl mode if (currState == movementState.crawl && !FPVModeCrawlControl && !FPVModeControl && inFPVModeCrawlTransition <= 0) { if (vel != Vector3.zero) { body.transform.Rotate(new Vector3(90f, 0f, 0f)); float velAngleFromCurrent = Vector3.Angle(body.transform.forward, vel); //dont turn snake around if its within 45 degrees if (Mathf.Abs(velAngleFromCurrent - 0f) <= 50f) { //body.transform.Rotate(new Vector3(-90f, 0f, 0f)); body.transform.rotation = Quaternion.LookRotation(vel.normalized); body.transform.Rotate(new Vector3(90f, 180f, 0f)); } else { body.transform.rotation = Quaternion.LookRotation(vel.normalized); body.transform.Rotate(new Vector3(90f, 0f, 0f)); } } } //Crouch toggle behavior if(Input.GetKeyDown(KeyCode.A)) { if(inCrouchMode) { inCrouchMode = false; body.transform.localScale = defaultPlayerSize; if(currState != movementState.sneak) { currState = movementState.run; } } else if (currState == movementState.run || currState == movementState.sneak) { //body.transform.Rotate(new Vector3(90f, 0f, 0f)); // body.transform.Rotate( Vector3.Lerp (Vector3.zero, new Vector3 (90f, 0f, 0f), 0.5f)); // body.transform.position = new Vector3(body.transform.position.x, .25f, body.transform.position.z); Vector3 crouchSize = body.transform.localScale; crouchSize.y = crouchHeight; body.transform.localScale = crouchSize; inCrouchMode = true; //special case when sneaking against wall if(currState == movementState.sneak) { currState = movementState.sneak; } else { currState = movementState.crawl; } } else if(currState == movementState.crawl && !FPVModeCrawlControl && inFPVModeCrawlTransition <= 0) { body.transform.Rotate(new Vector3(-90f, 0f, 0f)); body.transform.position = new Vector3(body.transform.position.x, .5f, body.transform.position.z); currState = movementState.run; } } //RAYCAST behaviors: //SNEAK Wall Detection if (vel != Vector3.zero) { //check for wall collision to stick to if (doubleRaycast(gameObject.transform.position, body.transform.forward, (gameObject.transform.lossyScale.z / 2) + 0.2f) && collided2 > 0) { collided2 -= Time.deltaTime; } else if (collided2 < 0 && currState == movementState.run) { // Flips character body.transform.rotation = Quaternion.LookRotation(vel.normalized); gameObject.transform.forward *= -1; currState = movementState.sneak; if (gameObject.transform.forward.x != 0) { moveLock = movementLock.LR; } else if (gameObject.transform.forward.z != 0) { moveLock = movementLock.FB; } } } //CRAWL modes DETECTION if (currState == movementState.crawl && !inCrouchMode) { //Check FPVModeCrawl State, aka if snake crawls under enclosed space, switch mode and camera if (Physics.Raycast(gameObject.transform.position, -body.transform.forward, (gameObject.transform.lossyScale.z / 2) + 1.0f, wallLayerMask)) { if (!FPVModeCrawlControl) { // Should only be FPV-Crawl when you cannot stand up aka crawling in a 1 meter tall tunnel CameraController.S.SwitchCameraTo(CameraType.crawl); FPVModeCrawlControl = true; inFPVModeCrawlTransition = ToFPVModeTransitionTime; lastForwardCrawlVector = body.velocity; } } else { if(FPVModeCrawlControl) { CameraController.S.SwitchCameraTo(CameraType.overhead); FPVModeCrawlControl = false; inFPVModeCrawlTransition = FromFPVModeTransitionTime; lastForwardCrawlVector = body.velocity; } } if(!FPVModeCrawlControl && !inCrouchMode) { Ray headray = new Ray(gameObject.transform.position, crawlForwardVector); RaycastHit hit; //Debug.DrawRay(gameObject.transform.position, crawlForwardVector * ((gameObject.transform.lossyScale.y / 2) + 0.4f), Color.green); if (Physics.Raycast(headray, out hit, ((gameObject.transform.lossyScale.y / 2) + 0.4f), wallLayerMask) && collided > 0) { collided -= Time.deltaTime; } else if (collided < 0) { // Flips character body.transform.rotation = Quaternion.LookRotation(vel.normalized); gameObject.transform.forward = -crawlForwardVector; currState = movementState.sneak; Vector3 newPos = hit.point; if(hit.point != Vector3.zero) { newPos += -crawlForwardVector.normalized * ((gameObject.transform.lossyScale.y / 4)); } else { newPos = gameObject.transform.position; } print(hit.point); gameObject.transform.position = newPos; Vector3 crouchSize = body.transform.localScale; crouchSize.y = crouchHeight; body.transform.localScale = crouchSize; inCrouchMode = true; if (gameObject.transform.forward.x != 0) { moveLock = movementLock.LR; } else if (gameObject.transform.forward.z != 0) { moveLock = movementLock.FB; } body.velocity = Vector3.zero; } Ray buttray = new Ray(gameObject.transform.position, -crawlForwardVector); RaycastHit hit2; Debug.DrawRay(gameObject.transform.position, -crawlForwardVector * (gameObject.transform.lossyScale.y / 4), Color.green); if (Physics.Raycast(buttray, out hit2, (gameObject.transform.lossyScale.y / 4), wallLayerMask) && collided2 > 0) { collided2 -= Time.deltaTime; } else if (collided2 < 0) { // Flips character body.transform.rotation = Quaternion.LookRotation(vel.normalized); gameObject.transform.forward = crawlForwardVector; currState = movementState.sneak; Vector3 newPos = hit2.point; if(newPos != Vector3.zero) { newPos += crawlForwardVector.normalized * ((gameObject.transform.lossyScale.y / 4)); } else { newPos = gameObject.transform.position; } gameObject.transform.position = newPos; Vector3 crouchSize = body.transform.localScale; crouchSize.y = crouchHeight; body.transform.localScale = crouchSize; inCrouchMode = true; if (gameObject.transform.forward.x != 0) { moveLock = movementLock.LR; } else if (gameObject.transform.forward.z != 0) { moveLock = movementLock.FB; } } } } //CRAWL modes DETECTION END }
// Update is called once per frame void Update() { if (seen) return; if (spawning_player) { growSpawnSphere(); } if (unspawning_player) { shrinkSpawnSphere(); } if (invincible_delay > 0) { invincible_delay -= Time.deltaTime; if (invincible_delay < 0) invincible_delay = 0; } if (Input.GetKey(GOD_KEY) && invincible_delay == 0) { god_mode = !god_mode; invincible_delay = 0.5f; GameObject head = gameObject.transform.GetChild(0).gameObject; if (god_mode) { head.GetComponent<Renderer>().material = spawn_sphere_material; } else { head.GetComponent<Renderer>().material = head_material; } } if (!CameraController.cam_control.playerHasControl() || CameraController.cam_control.game_paused) { body.velocity = Vector3.zero; return; } if (move_state == movementState.GRABBING) { resolveGrab(); } if (knock_lock || flip_lock) { return; } if (!lockControlsIfNeeded()) { if (under_obstacle_last_frame) { updateUnderObstacleTransformFromInput(); } else { setVelocityFromInput(); updateWallMovement(); updateForwardDirection(); } } keepPlayerWithinPositionLimits(); if (Input.GetKey(KNOCK_KEY)) { if (!knock_lock) { body.velocity = Vector3.zero; if (movementState.AGAINST_WALL == move_state) { knock_lock = true; Knock(); } else { punchCheck(); } Invoke("unlockKnock", 0.3f); //prevents knock spam } } if (Input.GetKey(GRAB_KEY)) { if (choke_timer <= 0) { resolveGrab(); if (move_state == movementState.GRABBING) { choke_count++; AudioController.audioPlayer.chokeSound(); body.velocity = Vector3.zero; knock_lock = true; victim.GetComponent<Rigidbody>().AddForceAtPosition(victim.transform.forward * -87, victim.transform.position + victim.transform.up); Invoke("unlockKnock", 0.1f); } choke_timer = 0.1f; } else { choke_timer -= Time.deltaTime; } if (choke_count > 10) { victim.GetComponent<Enemy>().die(); choke_count = 0; move_state = movementState.RUN; } } if (Input.GetKeyDown(CRAWL_KEY)) { toggleCrawl(); } adjustCamera(); adjustPlayerCollider(); velocity_last_frame = body.velocity; if (body.velocity.magnitude > 0) { if (move_state == movementState.RUN || move_state == movementState.ALONG_WALL) { AudioController.audioPlayer.stepSound(); } else if (move_state == movementState.CRAWL) { AudioController.audioPlayer.crawlSound(); } } }
//Determines the current movement state and what it should transition to protected IEnumerator computeState() { if (digging) { currentState = movementState.DIGGING; } int counter = 0; while (true) { switch (currentState) { case movementState.IDLE: animater.animationID = 2; if (currentMovement.x != 0) { currentState = movementState.WALKING; } break; case movementState.JUMPING: if (cc.isGrounded) { currentState = movementState.WALKING; counter = (int)landingDelay; } break; case movementState.CLIMBING: //TODO Set animation break; case movementState.WALKING: animater.animationID = 1; if (currentMovement.x == 0) { currentState = movementState.IDLE; } break; case movementState.DIGGING: animater.animationID = 0; emitParticles(); if (!digging) { currentState = movementState.IDLE; } break; } if (currentMovement.x < 0) { right = false; } if (currentMovement.x > 0) { right = true; } yield return(null); } }
// Update is called once per frame void Update() { timer += Time.deltaTime; uTimer += Time.fixedDeltaTime; if (slow) { if (uTimer > 2) { slow = false; SceneManager.LoadScene("CombatTest"); } } RaycastHit2D hit = Physics2D.Raycast(block.transform.position, -Vector2.up, 10); distance = (transform.position - player.transform.position).magnitude; switch (currentState) { case movementState.LEFT: if (!sleeping) { if (hit.collider != null) { anim.SetInteger("Riding", 1); ride = true; dir = -1f; } else if (hit.collider == null) { anim.SetInteger("Riding", 0); ride = false; transform.position += Vector3.right * 10 * Time.deltaTime; currentState = movementState.RIGHT; } transform.localScale = new Vector3(-0.5f, 0.5f, 1); } break; case movementState.RIGHT: if (!sleeping) { if (hit.collider != null) { anim.SetInteger("Riding", 1); ride = true; dir = 1f; } else if (hit.collider == null) { anim.SetInteger("Riding", 0); ride = false; transform.position += -Vector3.right * 10 * Time.deltaTime; currentState = movementState.LEFT; } transform.localScale = new Vector3(0.5f, 0.5f, 1); } break; case movementState.FOLLOW: if (!sleeping) { if (player.transform.position.x > transform.position.x) { transform.localScale = new Vector3(0.5f, 0.5f, 1); if (hit.collider != null) { anim.SetInteger("Riding", 1); ride = true; dir = 1f; } if (hit.collider == null) { anim.SetInteger("Riding", 0); rb.velocity = new Vector2(0, rb.velocity.y); ride = false; } } if (player.transform.position.x < transform.position.x) { if (hit.collider != null) { anim.SetInteger("Riding", 1); ride = true; dir = -1f; } transform.localScale = new Vector3(-0.5f, 0.5f, 1); if (hit.collider == null) { anim.SetInteger("Riding", 0); rb.velocity = new Vector2(0, rb.velocity.y); ride = false; } } if (player.transform.position.y > transform.position.y + 1) { if (grounded) { if (jumpReady) { anim.SetInteger("Riding", 3); anim.SetTrigger("Jumping"); jumpReady = false; rb.AddForce(Vector2.up * jumpForce); } } } else { if (grounded) { jumpReady = true; } } if (shoot && !sleeping) { if (timer >= 5) { GameObject projectile = Instantiate(electroBall, spawnPoint.transform.position, spawnPoint.transform.rotation); if (transform.localScale.x == 0.5f) { projectile.GetComponent <Projectile>().travelSpeed = 6; } if (transform.localScale.x == -0.5f) { projectile.GetComponent <Projectile>().travelSpeed = -6; } Destroy(projectile, 5f); timer = 0; } } } break; case movementState.SLEEP: anim.SetInteger("Riding", 9); break; } if (hit.collider != null) { if (hit.collider.gameObject.tag == "Ground") { if (distance < 12) { currentState = movementState.FOLLOW; } else { Search(); } } } }
// Update is called once per frame void Update() { if (seen) { return; } if (spawning_player) { growSpawnSphere(); } if (unspawning_player) { shrinkSpawnSphere(); } if (invincible_delay > 0) { invincible_delay -= Time.deltaTime; if (invincible_delay < 0) { invincible_delay = 0; } } if (Input.GetKey(GOD_KEY) && invincible_delay == 0) { god_mode = !god_mode; invincible_delay = 0.5f; GameObject head = gameObject.transform.GetChild(0).gameObject; if (god_mode) { head.GetComponent <Renderer>().material = spawn_sphere_material; } else { head.GetComponent <Renderer>().material = head_material; } } if (!CameraController.cam_control.playerHasControl() || CameraController.cam_control.game_paused) { body.velocity = Vector3.zero; return; } if (move_state == movementState.GRABBING) { resolveGrab(); } if (knock_lock || flip_lock) { return; } if (!lockControlsIfNeeded()) { if (under_obstacle_last_frame) { updateUnderObstacleTransformFromInput(); } else { setVelocityFromInput(); updateWallMovement(); updateForwardDirection(); } } keepPlayerWithinPositionLimits(); if (Input.GetKey(KNOCK_KEY)) { if (!knock_lock) { body.velocity = Vector3.zero; if (movementState.AGAINST_WALL == move_state) { knock_lock = true; Knock(); } else { punchCheck(); } Invoke("unlockKnock", 0.3f); //prevents knock spam } } if (Input.GetKey(GRAB_KEY)) { if (choke_timer <= 0) { resolveGrab(); if (move_state == movementState.GRABBING) { choke_count++; AudioController.audioPlayer.chokeSound(); body.velocity = Vector3.zero; knock_lock = true; victim.GetComponent <Rigidbody>().AddForceAtPosition(victim.transform.forward * -87, victim.transform.position + victim.transform.up); Invoke("unlockKnock", 0.1f); } choke_timer = 0.1f; } else { choke_timer -= Time.deltaTime; } if (choke_count > 10) { victim.GetComponent <Enemy>().die(); choke_count = 0; move_state = movementState.RUN; } } if (Input.GetKeyDown(CRAWL_KEY)) { toggleCrawl(); } adjustCamera(); adjustPlayerCollider(); velocity_last_frame = body.velocity; if (body.velocity.magnitude > 0) { if (move_state == movementState.RUN || move_state == movementState.ALONG_WALL) { AudioController.audioPlayer.stepSound(); } else if (move_state == movementState.CRAWL) { AudioController.audioPlayer.crawlSound(); } } }
//Determines the current movement state and what it should transition to protected IEnumerator computeState(){ if(digging){ currentState = movementState.DIGGING; animater.animationID = 0; } int counter = 0; while(true){ switch(currentState){ case movementState.IDLE: if(currentMovement.x != 0){ currentState = movementState.WALKING; animater.animationID = 1; } break; case movementState.JUMPING: if(onGround){ currentState = movementState.WALKING; animater.animationID = 1; } break; case movementState.LANDING: counter --; if(counter == 0){ currentState = movementState.WALKING; animater.animationID = 1; } break; case movementState.WALKING: if(!onGround && currentMovement.y != 0){ currentState = movementState.JUMPING; } else{ if(currentMovement.x == 0){ currentState = movementState.IDLE; animater.animationID = 2; } } break; case movementState.DIGGING: animater.animationID = 0; if(!digging){ currentState = movementState.IDLE; animater.animationID = 1; } break; } if(currentMovement.x < 0){ right = false; } if(currentMovement.x > 0){ right = true; } yield return null; } }
//Determines the current movement state and what it should transition to protected IEnumerator computeState() { if (digging) { currentState = movementState.DIGGING; animater.animationID = 0; } int counter = 0; while (true) { switch (currentState) { case movementState.IDLE: if (currentMovement.x != 0) { currentState = movementState.WALKING; animater.animationID = 1; } break; case movementState.JUMPING: if (onGround) { currentState = movementState.WALKING; animater.animationID = 1; } break; case movementState.LANDING: counter--; if (counter == 0) { currentState = movementState.WALKING; animater.animationID = 1; } break; case movementState.WALKING: if (!onGround && currentMovement.y != 0) { currentState = movementState.JUMPING; } else { if (currentMovement.x == 0) { currentState = movementState.IDLE; animater.animationID = 2; } } break; case movementState.DIGGING: animater.animationID = 0; if (!digging) { currentState = movementState.IDLE; animater.animationID = 1; } break; } if (currentMovement.x < 0) { right = false; } if (currentMovement.x > 0) { right = true; } yield return(null); } }
public void Start() { targetPos = transformObj.position; targetRot = transformObj.localRotation; transformObj.position = targetPos; stateMethod = idle; }
//****************************************************************************// void walkcase(IVector2 currentpos,IVector2 dest,ParentedNode node,Vector2 v){ if((v.y > .25f && Mathf.Abs(v.x) < .1f) || (map.getForeground(currentpos) == TileSpecList.getTileSpec(LADDERINDEX) && canLadder)){ laddercase(currentpos,dest,node,v); return; } currentState = movementState.WALKING; if(v.y > .25f && (Mathf.Abs(v.x) > .1f && cc.velocity.x == 0)){// || (lastPosition - currentpos).magnitude == 0){ Debug.Log("Jumping"); jump (); currentState = movementState.JUMPING; } }
void ladderTile(IVector2 t){ if(!canLadder) return; currentState = movementState.CLIMBING; TileSpec ts = map.getForeground(t); if(!ts.solid && ts != TileSpecList.getTileSpec(LADDERINDEX)){ map.setTile(t,(byte)LADDERINDEX,(byte)1); } }
private void bump() { if (input.verticalInput == 0) { stateMethod = idle; } }
//Handles the character digging. public void handleDigging(Vector2 dest,Vector2 v){ if(digging){ this.currentState = movementState.DIGGING; if(v.y < -.25){ jump(.75f); emitParticles(); } //If it is done digging if((digTimer -= Time.fixedDeltaTime) <= 0){ mine (dest); } } //Detect if it needs to start digging else if(map.isForegroundSolid(dest)){ digTimer = digTime; digging = true; } }
// Update is called once per frame void Update() { //grab the forwards vector for when the body is facedown (crawl state) Vector3 crawlForwardVector = findForwardCrawlVector(); //Debug.DrawRay(body.transform.position, crawlForwardVector * 5f, Color.green); if (Input.GetKeyDown(KeyCode.I)) { if (!bigBossMode) { bigBossMode = true; print("Invincibility Mode ACTIVATED!"); } else { bigBossMode = false; print("Invincibility Mode DE-ACTIVATED!"); } } if (currState == movementState.run || (inCrouchMode && currState == movementState.crawl) || currState == movementState.sneak) { if (Input.GetKeyDown(KeyCode.W)) { print("Triangle Button: FPV Cam"); FPVModeControl = true; CameraController.S.SwitchCameraTo(CameraType.fpv); } if (Input.GetKeyUp(KeyCode.W)) { FPVModeControl = false; CameraController.S.SwitchCameraTo(CameraType.overhead); } } Vector3 vel = Vector3.zero; if (Input.GetKey(KeyCode.UpArrow)) { vel.z += 1; } if (Input.GetKey(KeyCode.DownArrow)) { vel.z -= 1; } if (Input.GetKey(KeyCode.LeftArrow)) { vel.x -= 1; } if (Input.GetKey(KeyCode.RightArrow)) { vel.x += 1; } if (HandController.S.isFighting || HandController.S.isFighting || FPVModeControl) { vel = Vector3.zero; } // begin of FPV mode control GameObject fpvcamera = CameraController.S.cameras[(int)CameraType.fpv]; if (FPVModeControl) { //controls for FPV mode if (Input.GetKey(KeyCode.UpArrow)) { Quaternion rot = Quaternion.Euler(-45f, 0f, 0f); fpvcamera.transform.localRotation = Quaternion.Slerp(fpvcamera.transform.localRotation, rot, .05f); } if (Input.GetKey(KeyCode.DownArrow)) { Quaternion rot = Quaternion.Euler(45f, 0f, 0f); fpvcamera.transform.localRotation = Quaternion.Slerp(fpvcamera.transform.localRotation, rot, .05f); } if (Input.GetKey(KeyCode.LeftArrow)) { lastFPVForward = (new Vector3(0f, -FPVRotationSpeedDeg, 0f)); } else if (Input.GetKey(KeyCode.RightArrow)) { lastFPVForward = (new Vector3(0f, FPVRotationSpeedDeg, 0f)); } else { if (timeTillTurnUpdate <= 0) { lastFPVForward *= FPVRotationDecel; } } if (timeTillTurnUpdate > 0) { timeTillTurnUpdate -= Time.deltaTime; } else { timeTillTurnUpdate = 1f / 60f; body.transform.Rotate(lastFPVForward); } } if (fpvcamera.transform.localRotation != defaultFPVCameraAngle && !Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.UpArrow)) { fpvcamera.transform.localRotation = Quaternion.Slerp(fpvcamera.transform.localRotation, defaultFPVCameraAngle, .05f); } //perform wall stick sneak movements if (currState == movementState.sneak) { if (Vector3.Dot(vel, gameObject.transform.forward) > 0 && Vector3.Angle(vel, gameObject.transform.forward) < 90) { if (inCrouchMode) { currState = movementState.crawl; gameObject.transform.position += vel.normalized * sneakCrouchWallBuffer; } else { currState = movementState.run; } collided = collideToStick; collided2 = collideToStick * 2; } else if (!doubleRaycast(gameObject.transform.position, -body.transform.forward, (gameObject.transform.lossyScale.z / 2) + .2f)) { if (inCrouchMode) { currState = movementState.crawl; gameObject.transform.position += vel.normalized * sneakCrouchWallBuffer; } else { currState = movementState.run; } collided = collideToStick; collided2 = collideToStick * 2; } else if (inCrouchMode) { vel = Vector3.zero; } else if (moveLock == movementLock.LR) { vel.x = 0; } else if (moveLock == movementLock.FB) { vel.z = 0; } // SNEAK CAM CODE START // If sneaking, then check to see if you're at an edge Vector3 leftPosition, rightPosition; bool leftEdge, rightEdge; RaycastHit leftHit, rightHit; rightPosition = body.transform.position + body.transform.right + (body.transform.forward * -1); leftPosition = body.transform.position + (-1 * body.transform.right) + (body.transform.forward * -1); rightPosition.y += 3f; leftPosition.y += 3f; // Debug rays Debug.DrawRay(rightPosition, new Vector3(0f, -4f, 0f)); Debug.DrawRay(leftPosition, new Vector3(0f, -4f, 0f)); // change camera position to sneak cam position // If either of the raycasts don't hit anything, then player is at an edge leftEdge = Physics.Raycast(leftPosition, new Vector3(0f, -1f, 0f), out leftHit, 4f); rightEdge = Physics.Raycast(rightPosition, new Vector3(0f, -1f, 0f), out rightHit, 4f); if (leftEdge) { if (leftHit.collider.tag != "Floor") { leftEdge = false; } } if (rightEdge) { if (rightHit.collider.tag != "Floor") { rightEdge = false; } } if (leftEdge && rightEdge) { CameraController.S.GetComponent <CameraFollow> ().activateSneakCam(3, leftPosition + rightPosition); } else if (leftEdge) { CameraController.S.GetComponent <CameraFollow> ().activateSneakCam(0, leftPosition); } else if (rightEdge) { CameraController.S.GetComponent <CameraFollow> ().activateSneakCam(1, rightPosition); } // Otherwise, you're not at an edge else { // Change camera position back to normal CameraController.S.GetComponent <CameraFollow> ().deactivateSneakCam(); } // SNEAK CAM CODE END } // Also make sure that cam is deactivated to default once you leave sneak else { CameraController.S.GetComponent <CameraFollow> ().deactivateSneakCam(); } // Save the last known button press direction if (vel != Vector3.zero) { lastVel = vel; } //CROUCH logic: transform snake back to normal if he moves if (inCrouchMode && vel != Vector3.zero) { body.transform.localScale = defaultPlayerSize; //check for nearby walls so we don't clip inside them Ray frontray = new Ray(body.transform.position, body.transform.forward); //Ray buttray = new Ray(body.transform.position, -body.transform.forward); if (Physics.Raycast(frontray, body.transform.lossyScale.y / 2f, wallLayerMask)) { Vector3 safePos = gameObject.transform.position; safePos += -body.transform.forward.normalized * ((body.transform.lossyScale.y / 2f) + .1f); safePos.y = .2f; body.transform.position = safePos; } else { body.transform.position = new Vector3(body.transform.position.x, 0.2f, body.transform.position.z); } body.transform.rotation = Quaternion.LookRotation(vel.normalized); body.transform.Rotate(new Vector3(90f, 0f, 0f)); inCrouchMode = false; } //CRAWL box collider, shrink down the box collider when in crawl mode if (currState == movementState.crawl && !inCrouchMode) { boxcollider.size = new Vector3(boxcollider.size.x, 1f, boxcollider.size.z); boxcollider.center = new Vector3(boxcollider.center.x, .3f, boxcollider.center.z); } else { boxcollider.size = defaultBoxColliderSize; boxcollider.center = new Vector3(boxcollider.center.x, .15f, boxcollider.center.z); } //MOVEMENT of the Character // Speed and direction of movement depends on movementState if (inFPVModeCrawlTransition > 0 && currState == movementState.crawl) { inFPVModeCrawlTransition -= Time.deltaTime; body.velocity = lastForwardCrawlVector; } else if (HandController.S.isFighting || HandController.S.isGrabbing) { //don't set velocity here } else if (currState == movementState.run) { body.velocity = vel.normalized * speed; } else if (!FPVModeCrawlControl) { body.velocity = vel.normalized * slowSpeed; } else //first person crawl mode uses a different set of controls if (FPVModeCrawlControl) { if (Input.GetKey(KeyCode.UpArrow)) { body.velocity = crawlForwardVector * slowSpeed; } if (Input.GetKey(KeyCode.DownArrow)) { body.velocity = -crawlForwardVector * slowSpeed; } if (Input.GetKey(KeyCode.LeftArrow)) { lastCrawlTurnVector = (new Vector3(0f, 0f, FPVRotationSpeedDeg)); } else if (Input.GetKey(KeyCode.RightArrow)) { lastCrawlTurnVector = (new Vector3(0f, 0f, -FPVRotationSpeedDeg)); } else { if (timeTillTurnUpdate <= 0) { lastCrawlTurnVector *= FPVRotationDecel; } } if (timeTillTurnUpdate > 0) { timeTillTurnUpdate -= Time.deltaTime; } else { timeTillTurnUpdate = 1f / 60f; body.transform.Rotate(lastCrawlTurnVector); } }// end of FPV mode crawl control //ROTATIONS, face character towards the vector of movement: //set our foward direction if in run state (run rotation) if (currState == movementState.run && !FPVModeControl) { if (Vector3.Angle(body.transform.forward, lastVel.normalized) > 45f) { body.velocity = Vector3.zero; } // Lerp for smooth rotation body.transform.rotation = Quaternion.LookRotation(Vector3.Slerp(body.transform.forward, lastVel.normalized, rotationSpeed)); //body.transform.rotation = Quaternion.LookRotation(lastVel.normalized); } else //set look direction in crawl mode, unless snake is backing up, or in FPV Crawl mode if (currState == movementState.crawl && !FPVModeCrawlControl && !FPVModeControl && inFPVModeCrawlTransition <= 0) { if (vel != Vector3.zero) { body.transform.Rotate(new Vector3(90f, 0f, 0f)); float velAngleFromCurrent = Vector3.Angle(body.transform.forward, vel); //dont turn snake around if its within 45 degrees if (Mathf.Abs(velAngleFromCurrent - 0f) <= 50f) { //body.transform.Rotate(new Vector3(-90f, 0f, 0f)); body.transform.rotation = Quaternion.LookRotation(vel.normalized); body.transform.Rotate(new Vector3(90f, 180f, 0f)); } else { body.transform.rotation = Quaternion.LookRotation(vel.normalized); body.transform.Rotate(new Vector3(90f, 0f, 0f)); } } } //Crouch toggle behavior if (Input.GetKeyDown(KeyCode.A)) { if (inCrouchMode) { inCrouchMode = false; body.transform.localScale = defaultPlayerSize; if (currState != movementState.sneak) { currState = movementState.run; } } else if (currState == movementState.run || currState == movementState.sneak) { //body.transform.Rotate(new Vector3(90f, 0f, 0f)); // body.transform.Rotate( Vector3.Lerp (Vector3.zero, new Vector3 (90f, 0f, 0f), 0.5f)); // body.transform.position = new Vector3(body.transform.position.x, .25f, body.transform.position.z); Vector3 crouchSize = body.transform.localScale; crouchSize.y = crouchHeight; body.transform.localScale = crouchSize; inCrouchMode = true; //special case when sneaking against wall if (currState == movementState.sneak) { currState = movementState.sneak; } else { currState = movementState.crawl; } } else if (currState == movementState.crawl && !FPVModeCrawlControl && inFPVModeCrawlTransition <= 0) { body.transform.Rotate(new Vector3(-90f, 0f, 0f)); body.transform.position = new Vector3(body.transform.position.x, .5f, body.transform.position.z); currState = movementState.run; } } //RAYCAST behaviors: //SNEAK Wall Detection if (vel != Vector3.zero) { //check for wall collision to stick to if (doubleRaycast(gameObject.transform.position, body.transform.forward, (gameObject.transform.lossyScale.z / 2) + 0.2f) && collided2 > 0) { collided2 -= Time.deltaTime; } else if (collided2 < 0 && currState == movementState.run) { // Flips character body.transform.rotation = Quaternion.LookRotation(vel.normalized); gameObject.transform.forward *= -1; currState = movementState.sneak; if (gameObject.transform.forward.x != 0) { moveLock = movementLock.LR; } else if (gameObject.transform.forward.z != 0) { moveLock = movementLock.FB; } } } //CRAWL modes DETECTION if (currState == movementState.crawl && !inCrouchMode) { //Check FPVModeCrawl State, aka if snake crawls under enclosed space, switch mode and camera if (Physics.Raycast(gameObject.transform.position, -body.transform.forward, (gameObject.transform.lossyScale.z / 2) + 1.0f, wallLayerMask)) { if (!FPVModeCrawlControl) { // Should only be FPV-Crawl when you cannot stand up aka crawling in a 1 meter tall tunnel CameraController.S.SwitchCameraTo(CameraType.crawl); FPVModeCrawlControl = true; inFPVModeCrawlTransition = ToFPVModeTransitionTime; lastForwardCrawlVector = body.velocity; } } else { if (FPVModeCrawlControl) { CameraController.S.SwitchCameraTo(CameraType.overhead); FPVModeCrawlControl = false; inFPVModeCrawlTransition = FromFPVModeTransitionTime; lastForwardCrawlVector = body.velocity; } } if (!FPVModeCrawlControl && !inCrouchMode) { Ray headray = new Ray(gameObject.transform.position, crawlForwardVector); RaycastHit hit; //Debug.DrawRay(gameObject.transform.position, crawlForwardVector * ((gameObject.transform.lossyScale.y / 2) + 0.4f), Color.green); if (Physics.Raycast(headray, out hit, ((gameObject.transform.lossyScale.y / 2) + 0.4f), wallLayerMask) && collided > 0) { collided -= Time.deltaTime; } else if (collided < 0) { // Flips character body.transform.rotation = Quaternion.LookRotation(vel.normalized); gameObject.transform.forward = -crawlForwardVector; currState = movementState.sneak; Vector3 newPos = hit.point; if (hit.point != Vector3.zero) { newPos += -crawlForwardVector.normalized * ((gameObject.transform.lossyScale.y / 4)); } else { newPos = gameObject.transform.position; } print(hit.point); gameObject.transform.position = newPos; Vector3 crouchSize = body.transform.localScale; crouchSize.y = crouchHeight; body.transform.localScale = crouchSize; inCrouchMode = true; if (gameObject.transform.forward.x != 0) { moveLock = movementLock.LR; } else if (gameObject.transform.forward.z != 0) { moveLock = movementLock.FB; } body.velocity = Vector3.zero; } Ray buttray = new Ray(gameObject.transform.position, -crawlForwardVector); RaycastHit hit2; Debug.DrawRay(gameObject.transform.position, -crawlForwardVector * (gameObject.transform.lossyScale.y / 4), Color.green); if (Physics.Raycast(buttray, out hit2, (gameObject.transform.lossyScale.y / 4), wallLayerMask) && collided2 > 0) { collided2 -= Time.deltaTime; } else if (collided2 < 0) { // Flips character body.transform.rotation = Quaternion.LookRotation(vel.normalized); gameObject.transform.forward = crawlForwardVector; currState = movementState.sneak; Vector3 newPos = hit2.point; if (newPos != Vector3.zero) { newPos += crawlForwardVector.normalized * ((gameObject.transform.lossyScale.y / 4)); } else { newPos = gameObject.transform.position; } gameObject.transform.position = newPos; Vector3 crouchSize = body.transform.localScale; crouchSize.y = crouchHeight; body.transform.localScale = crouchSize; inCrouchMode = true; if (gameObject.transform.forward.x != 0) { moveLock = movementLock.LR; } else if (gameObject.transform.forward.z != 0) { moveLock = movementLock.FB; } } } } //CRAWL modes DETECTION END }
//Determines the current movement state and what it should transition to protected IEnumerator computeState(){ if(digging){ currentState = movementState.DIGGING; } int counter = 0; while(true){ switch(currentState){ case movementState.IDLE: animater.animationID = 2; if(currentMovement.x != 0){ currentState = movementState.WALKING; } break; case movementState.JUMPING: if(cc.isGrounded){ currentState = movementState.WALKING; counter = (int)landingDelay; } break; case movementState.CLIMBING: //TODO Set animation break; case movementState.WALKING: animater.animationID = 1; if(currentMovement.x == 0){ currentState = movementState.IDLE; } break; case movementState.DIGGING: animater.animationID = 0; emitParticles(); if(!digging){ currentState = movementState.IDLE; } break; } if(currentMovement.x < 0){ right = false; } if(currentMovement.x > 0){ right = true; } yield return null; } }
// Use this for initialization void Start() { myState = movementState.neutral; }
protected IEnumerator computeState() { int counter = 0; while (true) { if (pPhysics.climbing) { currentState = movementState.CLIMBING; } switch (currentState) { case movementState.IDLE: animater.animationID = 0; if (moveAmount.x != 0) { currentState = movementState.WALKING; } if (!pPhysics.onGround && moveAmount.y != 0) { currentState = movementState.JUMPING; } break; case movementState.JUMPING: animater.animationID = 2; if (pPhysics.onGround) { currentState = movementState.WALKING; } break; case movementState.WALKING: animater.animationID = 1; if (!pPhysics.onGround && moveAmount.y != 0) { currentState = movementState.JUMPING; } else { if (moveAmount.x == 0) { currentState = movementState.IDLE; } } break; case movementState.CLIMBING: animater.animationID = 3; if (!pPhysics.climbing) { currentState = movementState.WALKING; } break; } if (moveAmount.x < 0) { right = false; } if (moveAmount.x > 0) { right = true; } yield return(null); } }