- Home /
Question by
DJPC3 · Nov 06, 2020 at 10:16 PM ·
animationscripting problem
How can i impliment my animations (run to walk) in this script
If my speed is greater than 0.01 than he will run. If he jumps while grounded he will jump. The jump is correct. I just neeed a run.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour {
public Transform cam;
public CharacterController controller;
public float speed = 6f;
public float timeZeroToMax = 2.5f;
private float acceleratePerSec; private float forwardVelocity; private float turnSmoothVelocity;
public float turnSmoothTime = 0.1f;
bool jump = false;
public float gravity = -9.81f;
public float jumpHeight;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public Animator animator;
Vector3 velocity;
bool isGrounded;
float horizontalMove = 0f;
void Update()
{
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
animator.SetBool("IsJumping", true);
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
// animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir * speed * Time.deltaTime);
}
}
}
Comment