Correct reproduction of animation. Guys, written a script of movement from scratch. Began to add animation and there is such a deal. Let’s say we assigned certain keys to play certain animations. to the example: I ran forward, clicked to jump the running animation interrupted, I reproduced the animation of the jump, but after the jump I continued the animation of the run. And here’s where the correct reproduction of the animation idle.
Here are the scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {
[SerializeField]
private float speed = 5f;
[SerializeField]
private float lookSensitivity = 3f;
[SerializeField]
private float thrusterForce = 1000f;
private Animation animation;
private PlayerMotor motor;
private Rigidbody rb;
bool isGrounded = true;
void Start()
{
motor = GetComponent<PlayerMotor>();
animation = GetComponent<Animation>();
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Terrain"))
{
isGrounded = true;
}
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Terrain"))
{
isGrounded = false;
}
}
void Update()
{
float _xMov = Input.GetAxis("Horizontal");
float _zMov = Input.GetAxis("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
{
animation.Stop();
animation.CrossFade("run");
}
motor.Move(_velocity);
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yRot, 0f)*lookSensitivity;
motor.Rotate(_rotation);
float _xRot = Input.GetAxisRaw("Mouse Y");
float _cameraRotationX = _xRot * lookSensitivity;
motor.RotateCamera(_cameraRotationX);
Vector3 _thrusterForce = Vector3.zero;
if (Input.GetButton("Jump"))
{
if (isGrounded)
{
_thrusterForce = Vector3.up * thrusterForce;
animation.Stop();
animation.CrossFade("jump");
}
}
motor.ApplyThruster(_thrusterForce);
}
}
and
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour {
[SerializeField]
private Camera cam;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private float cameraRotationX = 0f;
private float currentCameraRotationX = 0f;
private Vector3 thrusterForce = Vector3.zero;
[SerializeField]
private float CameraRotationLimit = 85f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
public void Move(Vector3 _velocity)
{
velocity = _velocity;
}
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
public void RotateCamera(float _cameraRotationX)
{
cameraRotationX = _cameraRotationX;
}
public void ApplyThruster(Vector3 _thrusterForce)
{
thrusterForce = _thrusterForce;
}
void FixedUpdate()
{
PerformMovement();
PerformRotation();
}
void PerformMovement()
{
if(velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
if(thrusterForce != Vector3.zero)
{
rb.AddForce(Time.fixedDeltaTime * thrusterForce, ForceMode.Impulse);
}
}
void PerformRotation()
{
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
if (cam != null)
{
currentCameraRotationX -= cameraRotationX;
currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -CameraRotationLimit, CameraRotationLimit);
cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f);
}
}
}