// the Update loop contains a very simple example of moving the character around and controlling the animation void Update() { // grab our current _velocity to use as a base for all calculations _velocity = _controller.velocity; if (_controller.isGrounded) { _velocity.y = 0; } if (!goLeft) //Go Right { normalizedHorizontalSpeed = 1; if (transform.localScale.x < 0f) { transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z); } if (_controller.isGrounded) { _animator.Play(Animator.StringToHash("EnemyRun")); } } else //Go Left { normalizedHorizontalSpeed = -1; if (transform.localScale.x > 0f) { transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z); } if (_controller.isGrounded) { _animator.Play(Animator.StringToHash("EnemyRun")); } } // we can only jump whilst grounded /* if (_controller.isGrounded && Input.GetKeyDown(KeyCode.UpArrow)) * { * _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity); * _animator.Play(Animator.StringToHash("Jump")); * } */ // apply horizontal speed smoothing it var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction? _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor); // apply gravity before moving _velocity.y += gravity * Time.deltaTime; _controller.move(_velocity * Time.deltaTime); }
// the Update loop contains a very simple example of moving the character around and controlling the animation void Update() { if (m_controller.isGrounded) { m_groundedRemember = groundedRememberTime; m_velocity.y = 0; m_currentState = EPlayerState.Idle; m_gravity = gravity; if (!m_controller.collisionState.wasGroundedLastFrame) { if (m_audioSource && jumpClip) { m_audioSource.PlayOneShot(groundedClip); } StartCoroutine(ChangeScaleRoutine(spriteChild.localScale * m_groundingScaleMultiplier)); /* Instantiate Dust Particles */ InstantiateDustParticle(dustParticles, transform.position + (Vector3.down / 2f)); } } ProcessState(m_currentState); AnimationHandling(); // apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control var smoothedMovementFactor = m_controller.isGrounded ? groundDamping : inAirDamping; switch (m_currentState) { case EPlayerState.Idle: case EPlayerState.Moving: smoothedMovementFactor = groundDamping; break; case EPlayerState.Jumping: smoothedMovementFactor = inAirDamping; break; case EPlayerState.Dashing: smoothedMovementFactor = 0; break; } m_velocity.x = Mathf.Lerp(m_velocity.x, m_normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor); // apply gravity before moving m_velocity.y += m_gravity * Time.deltaTime; /* limiting gravity */ // m_velocity.y = Mathf.Max(m_gravity, m_velocity.y + (m_gravity * Time.deltaTime + (.5f * m_gravity * (Time.deltaTime * Time.deltaTime)))); m_controller.move(m_velocity * Time.deltaTime); m_velocity = m_controller.velocity; }
private void moveHorizontal(float xV) { charController.move(new Vector3(xV, 0)); }
// Update is called once per frame void FixedUpdate() { // grab our current velocity to use as a base for all calculations var velocity = _controller.velocity; if (_controller.isGrounded) { velocity.y = 0; jumpCount = 0; } //horizontal input if (Input.GetKey(KeyCode.D)) { normalizedHorizontalSpeed = 1; velocity.x = normalizedHorizontalSpeed * maxRunSpeed; if (transform.localScale.x < 0f) { transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z); } if (_controller.isGrounded) { _animator.SetBool("IsWalking", true); } } else if (Input.GetKey(KeyCode.A)) { normalizedHorizontalSpeed = -1; velocity.x = normalizedHorizontalSpeed * maxRunSpeed; if (transform.localScale.x > 0f) { transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z); } if (_controller.isGrounded) { _animator.SetBool("IsWalking", true); } } else { normalizedHorizontalSpeed = 0; velocity.x = normalizedHorizontalSpeed; if (_controller.isGrounded) { _animator.SetBool("IsWalking", false); } } if (Input.GetKeyDown(KeyCode.W)) { if (jumpCount < 2) { velocity.y = 20f; jumpCount++; _animator.SetBool("IsJumping", true); } // _animator.Play( Animator.StringToHash( "Jump" ) ); } velocity.y += gravity; _controller.move(velocity * Time.deltaTime); }