Question by
GuffeJuffe · Apr 05, 2021 at 08:59 AM ·
animationcharactercontrollerfloating
Adjust Charactercontroller size for landing animation?
I'm trying to add a jump and landing animation for my player. I'm using raycast to start the landing animation just before the player hits ground. However the Charactercontrollers hit box keeps the player off the ground so the raycast value needs to be too big and thus the animations starts too early. Anyone got at tip? :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonController : MonoBehaviour
{
public Camera MyCamera;
public float Speed = 5f;
public float SpringSpeed = 2f;
public float RotationSpeed = 15f;
public float AnimationBlendSpeed = 5f;
public float JumpSpeed = 15f;
CharacterController MyController;
Animator MyAnimator;
float mDesiredRotation = 0f;
float mDesiredAnimationSpeed = 0f;
float mSpeedY = 0;
float mGravity = -9.81f;
bool mSpringting = false;
bool mJumping = false;
void Start()
{
MyController = GetComponent<CharacterController>();
MyAnimator = GetComponent<Animator>();
}
void Update()
{
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
if (Input.GetButtonDown("Jump") && !mJumping)
{
mJumping = true;
MyAnimator.SetTrigger("Jump");
mSpeedY += JumpSpeed;
Debug.Log("Jump");
}
if (!MyController.isGrounded)
{
mSpeedY += mGravity * Time.deltaTime;
}
else if (mSpeedY < 0)
{
mSpeedY = 0;
}
MyAnimator.SetFloat("SpeedY", mSpeedY / JumpSpeed);
if (mJumping && mSpeedY < 0)
{
Debug.Log("mJumping && SY check");
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1.2f, LayerMask.GetMask("Default")))
{
mJumping = false;
MyAnimator.SetTrigger("Land");
Debug.Log("Landet");
}
}
mSpeedY += mGravity * Time.deltaTime;
mSpringting = Input.GetKey(KeyCode.LeftShift);
Vector3 verticalMovement = Vector3.up * mSpeedY;
Vector3 movement = new Vector3(x, 0, z).normalized;
Vector3 rotatedMovement = Quaternion.Euler(0, MyCamera.transform.rotation.eulerAngles.y, 0) * movement;
MyController.Move((verticalMovement + (rotatedMovement * (mSpringting ? SpringSpeed : Speed))) * Time.deltaTime);
if(rotatedMovement.magnitude > 0)
{
mDesiredRotation = Mathf.Atan2(rotatedMovement.x, rotatedMovement.z) * Mathf.Rad2Deg;
mDesiredAnimationSpeed = mSpringting ? 1 : 0.5f ;
}
else
{
mDesiredAnimationSpeed = 0;
}
MyAnimator.SetFloat("Speed", Mathf.Lerp(MyAnimator.GetFloat("Speed"), mDesiredAnimationSpeed, AnimationBlendSpeed * Time.deltaTime));
Quaternion currentRotation = transform.rotation;
Quaternion targetRotation = Quaternion.Euler(0, mDesiredRotation, 0);
transform.rotation = Quaternion.Lerp(currentRotation, targetRotation, RotationSpeed * Time.deltaTime);
}
}
skærmbillede-2021-04-05-100601.png
(88.9 kB)
skærmbillede-2021-04-05-102941.png
(48.9 kB)
Comment