protected virtual void UpdateFacing(Vector3 lookDir) { if (Mathf.Abs(lookDir.y) > Mathf.Abs(lookDir.x)) { if (lookDir.y < 0) { Facing = MovementAgent2D.LookDir.DOWN; return; } Facing = MovementAgent2D.LookDir.UP; return; } else if (lookDir.x < 0) { Facing = MovementAgent2D.LookDir.LEFT; return; } Facing = MovementAgent2D.LookDir.RIGHT; }
void GetMovingInput() { if (Input.GetKeyDown(KeyCode.W)) { _jumpPower = 16.0f; } else { _jumpPower = 0.0f; } if (Input.GetKey(KeyCode.S)) { _ani.SetBool("isSit", true); } else { _ani.SetBool("isSit", false); } if (Input.GetKey(KeyCode.D)) { if (!_ani.GetBool("isDash")) { _speed = 3.0f; } _ani.SetBool("isWalk", true); Dir = LookDir.Right; } else if (Input.GetKey(KeyCode.A)) { if (!_ani.GetBool("isDash")) { _speed = 3.0f; } _ani.SetBool("isWalk", true); Dir = LookDir.Left; } else { _ani.SetBool("isWalk", false); _ani.SetBool("isDash", false); _speed = 0.0f; } }
void Awake() { _ani = GetComponent <Animator>(); _ani.speed = 1.2f; _sprite = GetComponent <SpriteRenderer>(); _rigid = GetComponent <Rigidbody2D>(); _scale = new Vector3(3.0f, 3.0f, 3.0f); _scaleFlippedX = new Vector3(-3.0f, 3.0f, 3.0f); _inputHandler = new Dictionary <KeyCode, Action>(); _speed = 0.0f; _jumpPower = 0.0f; _isFocusing = false; _isJumping = false; DirVec = Vector3.zero; Dir = LookDir.Right; }
/** Calculates desired velocity. * Finds the target path segment and returns the forward direction, scaled with speed. * A whole bunch of restrictions on the velocity is applied to make sure it doesn't overshoot, does not look too far ahead, * and slows down when close to the target. * /see speed * /see endReachedDistance * /see slowdownDistance * /see CalculateTargetPoint * /see targetPoint * /see targetDirection * /see currentWaypointIndex */ protected Vector3 CalculateVelocity(Vector3 currentPosition) { if (path == null || path.vectorPath == null || path.vectorPath.Count == 0) { return(Vector3.zero); } List <Vector3> vPath = path.vectorPath; if (vPath.Count == 1) { vPath.Insert(0, currentPosition); } if (currentWaypointIndex >= vPath.Count) { currentWaypointIndex = vPath.Count - 1; } if (currentWaypointIndex <= 1) { currentWaypointIndex = 1; } while (true) { if (currentWaypointIndex < vPath.Count - 1) { //There is a "next path segment" float dist = XZSqrMagnitude(vPath[currentWaypointIndex], currentPosition); //Mathfx.DistancePointSegmentStrict (vPath[currentWaypointIndex+1],vPath[currentWaypointIndex+2],currentPosition); if (dist < pickNextWaypointDist * pickNextWaypointDist) { lastFoundWaypointPosition = currentPosition; lastFoundWaypointTime = Time.time; currentWaypointIndex++; } else { break; } } else { break; } } Vector3 dir = vPath[currentWaypointIndex] - vPath[currentWaypointIndex - 1]; Vector3 targetPosition = CalculateTargetPoint(currentPosition, vPath[currentWaypointIndex - 1], vPath[currentWaypointIndex]); dir = targetPosition - currentPosition; dir.y = 0; float targetDist = dir.magnitude; float slowdown = Mathf.Clamp01(targetDist / slowdownDistance); this.targetDirection = dir; this.targetPoint = targetPosition; if (currentWaypointIndex == vPath.Count - 1 && targetDist <= endReachedDistance) { if (!targetReached) { targetReached = true; OnTargetReached(); } //Send a move request, this ensures gravity is applied return(Vector3.zero); } Vector3 forward; float dot; if (LookDir.Equals(Vector3.zero)) { forward = tr.forward; dot = Vector3.Dot(dir.normalized, forward); } else { forward = dir.normalized; dot = 1; } float sp = speed * Mathf.Max(dot, minMoveScale) * slowdown; #if ASTARDEBUG Debug.DrawLine(vPath[currentWaypointIndex - 1], vPath[currentWaypointIndex], Color.black); Debug.DrawLine(GetFeetPosition(), targetPosition, Color.red); Debug.DrawRay(targetPosition, Vector3.up, Color.red); Debug.DrawRay(GetFeetPosition(), dir, Color.yellow); Debug.DrawRay(GetFeetPosition(), forward * sp, Color.cyan); #endif if (Time.deltaTime > 0) { sp = Mathf.Clamp(sp, 0, targetDist / (Time.deltaTime * 2)); } return(forward * sp); }
void Update() { if (mapLength != floorCreation.floorTileLength - 1) { mapLength = floorCreation.floorTileLength - 1; } if (mapWidth != floorCreation.floorTileWidth - 1) { mapWidth = floorCreation.floorTileWidth - 1; } //Get left and right movement float horizontalMovement = (Input.GetAxis("Horizontal") * movementSpeed) * Time.deltaTime; //Get up and down movement float verticalMovement = (Input.GetAxis("Vertical") * movementSpeed) * Time.deltaTime; //Rotate the mesh controller to make the player rotate towards the direction they are going. if (Input.GetAxis("Horizontal") > 0) { if (lookDir != LookDir.right) { armatureMesh.transform.rotation = Quaternion.LookRotation(Vector3.right); lookDir = LookDir.right; } } if (Input.GetAxis("Horizontal") < 0) { if (lookDir != LookDir.left) { armatureMesh.transform.rotation = Quaternion.LookRotation(Vector3.left); lookDir = LookDir.left; } } if (Input.GetAxis("Vertical") < 0) { if (lookDir != LookDir.backwards) { armatureMesh.transform.rotation = Quaternion.LookRotation(Vector3.back); lookDir = LookDir.backwards; } } if (Input.GetAxis("Vertical") > 0) { if (lookDir != LookDir.forwards) { armatureMesh.transform.rotation = Quaternion.LookRotation(Vector3.forward); lookDir = LookDir.forwards; } } //Use Normal activate/deactivate ability if (Input.GetButtonDown("Fire1")) { //When left click is pressed highlight the tile underneath the player if no other tiles are marked if (!tileMarked) { MarkFloorCube(); } else if (tileMarked) { //If a tile is marked, unmark the tile and deactivate the cube above it ActivateMarkedCube(markedTileCoord, false); } } //activate special ability if (Input.GetButtonDown("Fire2")) { //Special ability activates all of the special marked cubes (To array to make sure the list isnt changed throughout the loop) foreach (Vector2 coord in gameManager.specialBlockCoords.ToArray()) { ActivateMarkedCube(coord, true); } } if (Input.GetButtonDown("Jump")) { gameManager.ReduceMovementTime(); } if (Input.GetButtonUp("Jump")) { gameManager.RestoreMovementTime(); } if (horizontalMovement != 0 || verticalMovement != 0) { animator.SetBool("IsMoving", true); } else { animator.SetBool("IsMoving", false); } if (isFalling) { animator.SetBool("IsFalling", true); } else { animator.SetBool("IsFalling", false); } transform.Translate(horizontalMovement, 0, verticalMovement); CheckFloorCube(); //Constrains the player to the map if (transform.position.x < 0) { transform.position = new Vector3(0, transform.position.y, transform.position.z); } if (transform.position.x > mapWidth) { transform.position = new Vector3(mapWidth, transform.position.y, transform.position.z); } //If the player has fallen off end the game if (transform.position.y < 0.2) { LevelWaveEndScreen.text = "Level: " + (gameManager.level + 1) + " Wave: " + gameManager.wave; gameOverPanel.SetActive(true); } //Constrains the player if they aren't falling if (!isFalling) { if (transform.position.z > 0) { transform.position = new Vector3(transform.position.x, transform.position.y, 0); } if (transform.position.z < -mapLength) { transform.position = new Vector3(transform.position.x, transform.position.y, -mapLength); } //safety for just incase the player gets stuck in a floor tile when running from falling cubes. if (transform.position.y < startPos.y) { transform.position = new Vector3(transform.position.x, startPos.y, transform.position.z); } } }