i have a question i don´t find the right answer.
Iám new in coding and i try to make my first character and want to do and know about good controlling and animations first.
But i´am stucking in an “error”. The Character ist working so far but i didn´t know how i add a new animation like “dodge” or “shoot” because it will always run an an old animation or in the dodge animation the character will jump und stuff like that. I really didn´t know if iám using the wrong code or i have an simple error.
I hope someone could help.
I will add my movement.cs so you can see the whole script.
The “error” is on the //Character is dodging where i wanna start the dodge animation
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
private float rotationSpeed;
[SerializeField]
private float jumpHeight;
[SerializeField]
private float gravityMultiplier;
[SerializeField]
private float jumpHorizontalSpeed;
[SerializeField]
private float jumpButtonGracePeriod;
[SerializeField]
private float dodgeButtonGracePeriod;
[SerializeField]
private Transform cameraTransform;
private Animator animator;
private CharacterController characterController;
private float ySpeed;
private float originalStepOffset;
private float? lastGroundedTime;
private float? jumpButtonPressedTime;
private float? dodgeButtonPressedTime;
private bool isJumping;
private bool isGrounded;
private bool isDodging;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
originalStepOffset = characterController.stepOffset;
}
// Update is called once per frame
void Update()
{
//Character Movement
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
float inputMagnitude = Mathf.Clamp01(movementDirection.magnitude);
//Movement Slow
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
inputMagnitude /= 2;
}
//Blend of Animation - Idle,Walk, run - BlendTreeMovement
animator.SetFloat("Input Magnitude", inputMagnitude, 0.05f, Time.deltaTime);
//Movement Z & X Direction
movementDirection = Quaternion.AngleAxis(cameraTransform.rotation.eulerAngles.y, Vector3.up) * movementDirection;
movementDirection.Normalize();
//Speed Y-Direction based on gravity & jumpButtonpressed
float gravity = Physics.gravity.y * gravityMultiplier;
ySpeed += gravity * Time.deltaTime;
if (characterController.isGrounded)
{
lastGroundedTime = Time.time;
}
if (Input.GetButtonDown("Jump"))
{
jumpButtonPressedTime = Time.time;
}
if (Time.time - lastGroundedTime <= jumpButtonGracePeriod)
{
characterController.stepOffset = originalStepOffset;
ySpeed = -0.5f;
animator.SetBool("isGrounded", true);
isGrounded = true;
animator.SetBool("isJumping", false);
isJumping = false;
animator.SetBool("isFalling", false);
animator.SetBool("isDodging", false);
isDodging = false;
if (Time.time - jumpButtonPressedTime <= jumpButtonGracePeriod)
{
ySpeed = Mathf.Sqrt(jumpHeight * -3 * gravity);
animator.SetBool("isJumping", true);
isJumping = true;
jumpButtonPressedTime = null;
lastGroundedTime = null;
}
}
else
{
characterController.stepOffset = 0;
animator.SetBool("isGrounded", false);
isGrounded = false;
if ((isJumping && ySpeed < 0) || ySpeed < -2)
{
animator.SetBool("isFalling", true);
}
}
//Character isDodging NOT RIGHT -> HERE IS THE ERROR !
if (Input.GetButtonDown("Dodge"))
{
dodgeButtonPressedTime = Time.time;
}
if (Time.time - lastGroundedTime <= dodgeButtonGracePeriod)
{
characterController.stepOffset = originalStepOffset;
ySpeed = -0.5f;
animator.SetBool("isGrounded", true);
isGrounded = true;
animator.SetBool("isJumping", false);
isJumping = false;
animator.SetBool("isDodging", false);
isDodging = false;
animator.SetBool("isFalling", false);
if (Time.time - dodgeButtonPressedTime <= dodgeButtonGracePeriod)
{
ySpeed = 2;
animator.SetBool("isDodging", true);
isDodging = true;
dodgeButtonPressedTime = null;
lastGroundedTime = null;
}
}
else
{
characterController.stepOffset = 0;
animator.SetBool("isGrounded", false);
isGrounded = false;
if ((isDodging && ySpeed < -0.1) || ySpeed < -2)
{
animator.SetBool("isDodging", true);
}
}
//Character isMoving
if (movementDirection != Vector3.zero)
{
animator.SetBool("isMoving", true);
Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
else
{
animator.SetBool("isMoving", false);
}
if (isGrounded == false)
{
Vector3 velocity = movementDirection * inputMagnitude * jumpHorizontalSpeed;
velocity.y = ySpeed;
characterController.Move(velocity * Time.deltaTime);
}
}
//Slope Methode
private Vector3 AdjustVelocityToSlope(Vector3 velocity)
{
var ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out RaycastHit hitInfo, 0.2f))
{
var slopeRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
var adjustedVelocity = slopeRotation * velocity;
if (adjustedVelocity.y < 0)
{
return adjustedVelocity;
}
}
return velocity;
}
//Movement Character-Controller - RootMotion
private void OnAnimatorMove()
{
if (isGrounded)
{
Vector3 velocity = animator.deltaPosition;
velocity = AdjustVelocityToSlope(velocity);
//Charactermovement same speed every Time/Frame
velocity.y += ySpeed * Time.deltaTime;
characterController.Move(velocity);
}
}
private void OnApplicationFocus(bool focus)
{
if (focus)
{
Cursor.lockState = CursorLockMode.Locked;
}
else
{
Cursor.lockState = CursorLockMode.None;
}
}
}
i need help for the script to trigger the right animation without jumping or dead end
Schauhi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.