Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Animation

Discussion in 'Animation' started by HelloLight, Mar 10, 2023.

  1. HelloLight

    HelloLight

    Joined:
    Jul 28, 2022
    Posts:
    1
    My code won't work.
    My code is for animating a lift.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class LiftSet : MonoBehaviour
    {

    Code (CSharp):
    1.     private Animator animator;
    2.     private bool touchingPlayer = false;
    3.  
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         animator = gameObject.GetComponent<Animator>();
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (touchingPlayer)
    14.         {
    15.             animator.SetBool("TouchingPlayer_bool", true);
    16.         }
    17.     }
    18.  
    19.     void OnCollisionEnter(Collision collision)
    20.     {
    21.         if (collision.gameObject.CompareTag("Player"))
    22.         {
    23.             touchingPlayer = true;
    24.         }
    25.     }
    26. }
    27.  
    I don't understand what is going on.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Put Debug.Log() lines in your code to see if it's actually running in the console.

    Like this for example:
    Code (CSharp):
    1.     void OnCollisionEnter(Collision collision)
    2.     {
    3.         if (collision.gameObject.CompareTag("Player"))
    4.         {
    5.             touchingPlayer = true;
    6.  
    7.             Debug.Log("Collided with player.");
    8.         }
    9.     }
    Also, check your animator at runtime to see if the bool is changing to true. There could be a typo in the string reference ("TouchingPlayer_bool").