I created a push animation where the player is SetActive(“isPushing”, true) if it’s colliding with a object with a tag “Box”. But the problem is when the player is NO longer pushing the object or colliding with the object the Player doesn’t go back to it’s Idle or Walk animation Etc. It stays in its Push animation only when if the Object has touch the Pressure Pad when the Object Box turns Green Then the Player goes back to it’s Idle or Walk animation etc.
I was following this tutorial on how to push a cube to a pressure pad:
Do I have to make a Script for the Object with the Tag “Box” is order make the push Animation go back to idle or walk if the player is not colliding with the Object? If so how can I do it?
Below are screenShots of the Player’s Animator and a Video of the Situation
Pushing Object Animation Issue:
Images of the Animator of Player
Transition from State to Push
Transition from Push to Idle
// EzmiControl Script The Push code starts in line 203 and 286 and 317
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class EzmiControl : MonoBehaviour
{
private CharacterController controller;
public Transform cam;
[SerializeField] public Animator anim;
private Vector2 movementInput;
public Vector3 currentMovement;
[SerializeField]
private Vector3 appliedMovement;
public float gravity;
public float groundedGravity = 0f;
public float jumpHeight = 3f;
public float speed = 30f;
private bool isMovementPressed;
[SerializeField]
private bool isGrounded;
private bool isRunningPressed;
private bool isJumping;
//private bool isJumpPressed = false;
public float rotationFactor;
//push objects with Character Controller
[SerializeField]
private float pushForce;
private bool isCollidingWithBox = false;
//Player Health
[SerializeField]
private int playerHealth = 100;
[SerializeField]
private HealthBarFillTest healthBar;
private bool isTakingDamage = false;
[Header("Debug")]
public float gravityLerpStrength = 1.0f;
public bool movementIsCameraRelative = true;
float turnSmoothVelocity;
public float turnSmoothTime = 0.1f;
public Coroutine damageCoroutine;
//
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
//cam = Camera.main.transform;
rotationFactor = 10f;
gravity = -32f;
}
private void HandleHorizontalMovement()
{
movementInput = InputManager.instance.GetMoveDirection();
currentMovement.x = movementInput.x;
currentMovement.z = movementInput.y;
isRunningPressed = InputManager.instance.GetIsRunning();
if (isRunningPressed)
{
int runMultiplier = 3;
appliedMovement.x = currentMovement.x * speed * runMultiplier;
appliedMovement.z = currentMovement.z * speed * runMultiplier;
}
else
{
appliedMovement.x = currentMovement.x * speed;
appliedMovement.z = currentMovement.z * speed;
}
isMovementPressed = currentMovement.x != 0 || currentMovement.z != 0;
}
private void HandleRotation()
{
//isRunning = InputManager.instance.GetRunPressed();
movementInput = InputManager.instance.GetMoveDirection();
float horizontal = movementInput.x;
float vertical = movementInput.y;
bool isWalking = isMovementPressed;
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
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;
isRunningPressed = InputManager.instance.GetIsRunning();
if (isRunningPressed)
{
int runMultiplier = 3;
appliedMovement.x = moveDir.x * speed * runMultiplier;
appliedMovement.z = moveDir.z * speed * runMultiplier;
}
else
{
appliedMovement.x = moveDir.x * speed;
appliedMovement.z = moveDir.z * speed;
}
}
}
void HandleAnimation()
{
bool isWalking = anim.GetBool("isWalking");
bool isRunning = anim.GetBool("isRunning");
if (isMovementPressed && !isWalking)
{
anim.SetBool("isWalking", true);
}
else if (!isMovementPressed && isWalking)
{
anim.SetBool("isWalking", false);
}
if ((isMovementPressed && isRunningPressed) && !isRunning)
{
anim.SetBool("isRunning", true);
}
else if ((!isMovementPressed || !isRunningPressed) && isRunning)
{
anim.SetBool("isRunning", false);
}
}
private void HandleGravity()
{
if (!isGrounded)
{
appliedMovement.y = Mathf.Lerp(appliedMovement.y, gravity, gravityLerpStrength * Time.deltaTime);
}
else if (isGrounded && Mathf.Abs(appliedMovement.y) < 5f)
{
appliedMovement.y = groundedGravity;
}
}
private void HandleJump()
{
isJumping = InputManager.instance.GetIsJumping();
if (isGrounded && isJumping)
{
anim.SetBool("isJumping", true);
Debug.Log("Changing applied Movement");
appliedMovement.y = jumpHeight;
}
else if (!isJumping) // Comment this out later
{
anim.SetBool("isJumping", false);
}
if (isGrounded)
{
anim.speed = 1;
}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody rb = hit.collider.attachedRigidbody;
if (rb != null && !rb.isKinematic)
{
rb.velocity = hit.moveDirection * pushForce;
if (hit.gameObject.CompareTag("Box"))
{
isCollidingWithBox = true;
anim.SetBool("isPushing", true);
}
else
{
anim.SetBool("isPushing", false);
}
}
//if (hit.gameObject.tag == "Health")
//{
// IncreaseHealth(10);
//}
if (hit.gameObject.tag == "Enemy")
{
Debug.Log("Damage");
if (damageCoroutine == null)
{
damageCoroutine = StartCoroutine(DamageCoroutine(10));
}
}
else if (hit.gameObject.tag == "Health")
{
IncreaseHealth(10);
}
//IncreaseHealth(100);
}
public IEnumerator DamageCoroutine(int damage)
{
if (!isTakingDamage)
{
isTakingDamage = true;
playerHealth -= damage;
}
if (playerHealth <= 0)
{
Debug.Log("Player Has Died");
}
// Update health bar or other UI elements
if (healthBar != null)
{
healthBar.Deduct(damage);
// Add Hit Animation
anim.SetTrigger("isTakingDamage");
}
yield return new WaitForSeconds(1.5f); // Adjust the delay as needed
isTakingDamage = false;
yield return null;
}
public void IncreaseHealth(int amount)
{
playerHealth += amount;
if (playerHealth > 100)
{
playerHealth = 100;
}
// Update health bar or other UI elements
if (healthBar != null)
{
healthBar.Add(amount);
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Box"))
{
isCollidingWithBox = false;
}
}
// Update is called once per frame
void Update()
{
if (DialogueManager.instance.dialogueIsPlaying)
{
anim.SetBool("isWalking", false);
anim.SetBool("isRunning", false);
return;
}
isGrounded = controller.isGrounded;
HandleHorizontalMovement();
HandleRotation();
HandleGravity();
HandleJump();
HandleAnimation();
controller.Move(appliedMovement * Time.deltaTime);
if (!isCollidingWithBox)
{
// If not colliding with box, set isPushing to false
anim.SetBool("isPushing", false);
}
}
}
// PressurePad Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PressurePad : MonoBehaviour
{
private MeshRenderer meshRend;
public bool isAllActive = false;
public GameObject heartShard;
public bool isPadActive { get; private set; } = false;
private void Start()
{
meshRend = GetComponent<MeshRenderer>();
if(meshRend == null)
{
Debug.Log("MeshRenderer is Null");
}
}
private void OnTriggerStay(Collider other)
{
if(other.CompareTag("Box"))
{
Transform box = other.GetComponent<Transform>();
float distance = Vector3.Distance(box.position, transform.position);
if(distance < 12.11f)
{
Debug.Log("It is Place");
box.GetComponent<Rigidbody>().isKinematic = true;
box.GetComponent<Renderer>().material.color = Color.green;
Destroy(box.gameObject, 2f);
FindFirstObjectByType<EzmiControl>().anim.SetBool("isPushing", false); //Make the anim in EzmiControl public
isPadActive = true;
ControlHeartPiece();
}
}
}
private void ControlHeartPiece()
{
PressurePad otherPad = FindObjectOfType<PressurePad>();
if(isPadActive && otherPad.isPadActive)
{
heartShard.SetActive(true);
}
}
}