Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CharacterMovement : MonoBehaviour
- {
- public float floorOffsetY;
- public float moveSpeed = 6f;
- public float rotateSpeed = 10f;
- public float slopeLimit = 45f;
- public float slopeInfluence = 5f;
- public float jumpPower = 10f;
- Rigidbody rb;
- Animator anim;
- float vertical;
- float horizontal;
- Vector3 moveDirection;
- float inputAmount;
- Vector3 raycastFloorPos;
- Vector3 floorMovement;
- Vector3 gravity;
- Vector3 CombinedRaycast;
- float jumpFalloff = 2f;
- bool jump_input_down;
- float slopeAmount;
- Vector3 floorNormal;
- // Use this for initialization
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- anim = GetComponentInChildren<Animator>();
- }
- private void Update()
- {
- // reset movement
- moveDirection = Vector3.zero;
- // get vertical and horizontal movement input (controller and WASD/ Arrow Keys)
- vertical = Input.GetAxis("Vertical");
- horizontal = Input.GetAxis("Horizontal");
- jump_input_down = Input.GetKeyDown(KeyCode.Space);
- // base movement on camera
- Vector3 correctedVertical = vertical * Camera.main.transform.forward;
- Vector3 correctedHorizontal = horizontal * Camera.main.transform.right;
- Vector3 combinedInput = correctedHorizontal + correctedVertical;
- // normalize so diagonal movement isnt twice as fast, clear the Y so your character doesnt try to
- // walk into the floor/ sky when your camera isn't level
- moveDirection = new Vector3((combinedInput).normalized.x, 0, (combinedInput).normalized.z);
- // make sure the input doesnt go negative or above 1;
- float inputMagnitude = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
- inputAmount = Mathf.Clamp01(inputMagnitude);
- // rotate player to movement direction
- Quaternion rot = Quaternion.LookRotation(moveDirection);
- Quaternion targetRotation = Quaternion.Slerp(transform.rotation, rot, Time.fixedDeltaTime * inputAmount * rotateSpeed);
- transform.rotation = targetRotation;
- if (jump_input_down)
- {
- Jump();
- }
- // handle animation blendtree for walking
- anim.SetFloat("Velocity", inputAmount, 0.2f, Time.deltaTime);
- anim.SetFloat("SlopeNormal", slopeAmount, 0.2f, Time.deltaTime);
- }
- private void FixedUpdate()
- {
- // if not grounded , increase down force
- if (!IsGrounded() || slopeAmount >= 0.1f)// if going down, also apply, to stop bouncing
- {
- gravity += Vector3.up * Physics.gravity.y * jumpFalloff * Time.fixedDeltaTime;
- }
- // actual movement of the rigidbody + extra down force
- rb.velocity = (moveDirection * GetMoveSpeed() * inputAmount) + gravity;
- // find the Y position via raycasts
- floorMovement = new Vector3(rb.position.x, FindFloor().y + floorOffsetY, rb.position.z);
- // only stick to floor when grounded
- if (floorMovement != rb.position && IsGrounded() && rb.velocity.y <= 0)
- {
- // move the rigidbody to the floor
- rb.MovePosition(floorMovement);
- gravity.y = 0;
- }
- }
- Vector3 FindFloor()
- {
- // width of raycasts around the centre of your character
- float raycastWidth = 0.25f;
- // check floor on 5 raycasts , get the average when not Vector3.zero
- int floorAverage = 1;
- CombinedRaycast = FloorRaycasts(0, 0, 1.6f);
- floorAverage += (getFloorAverage(raycastWidth, 0) + getFloorAverage(-raycastWidth, 0) + getFloorAverage(0, raycastWidth) + getFloorAverage(0, -raycastWidth));
- return CombinedRaycast / floorAverage;
- }
- // only add to average floor position if its not Vector3.zero
- int getFloorAverage(float offsetx, float offsetz)
- {
- if (FloorRaycasts(offsetx, offsetz, 1.6f) != Vector3.zero)
- {
- CombinedRaycast += FloorRaycasts(offsetx, offsetz, 1.6f);
- return 1;
- }
- else { return 0; }
- }
- bool IsGrounded()
- {
- if (FloorRaycasts(0, 0, 0.6f) != Vector3.zero)
- {
- slopeAmount = Vector3.Dot(transform.forward, floorNormal);
- return true;
- }
- else
- {
- return false;
- }
- }
- Vector3 FloorRaycasts(float offsetx, float offsetz, float raycastLength)
- {
- RaycastHit hit;
- // move raycast
- raycastFloorPos = transform.TransformPoint(0 + offsetx, 0 + 0.5f, 0 + offsetz);
- Debug.DrawRay(raycastFloorPos, Vector3.down, Color.magenta);
- if (Physics.Raycast(raycastFloorPos, -Vector3.up, out hit, raycastLength))
- {
- floorNormal = hit.normal;
- if (Vector3.Angle(floorNormal, Vector3.up) < slopeLimit)
- {
- return hit.point;
- }
- else return Vector3.zero;
- }
- else return Vector3.zero;
- }
- float GetMoveSpeed()
- {
- // you can add a run here, if run button : currentMovespeed = runSpeed;
- float currentMovespeed = Mathf.Clamp(moveSpeed + (slopeAmount * slopeInfluence), 0, moveSpeed + 1);
- return currentMovespeed;
- }
- void Jump()
- {
- if (IsGrounded())
- {
- gravity.y = jumpPower;
- anim.SetTrigger("Jumping");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment