Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Animation Object

Discussion in 'Scripting' started by xaviza11, Jul 23, 2020.

  1. xaviza11

    xaviza11

    Joined:
    Jul 23, 2020
    Posts:
    3
    Hi guys I'm try to animate one object when I press "Q" inside of a trigger, but my script doesn't work, how can I do?

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class animador : MonoBehaviour
    {
    Boolean puertaabierta;

    private void OnTriggerStay(Collider other)
    {
    if (other.tag == "actmodulo") {
    if (Input.GetKeyDown(KeyCode.Q))
    mueble.GetComponent<Animation>().Play("modulo");
    if (puertaabierta == true)
    puertaabierta = false;
    if (puertaabierta == false)
    puertaabierta = true;

    }
    }


    // Start is called before the first frame update
    void Start()
    {
    puertaabierta = false;
    }

    // Update is called once per frame
    void Update()
    {

    }


    public GameObject mueble;


    }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Hey and welcome. For one you can post code using code tags, so others can read it. Using a language others understand is also a good idea. As a rule of thumb, coding should be done in english, at least if anybody else is ever going to read the code who is not native in the used language. Which is mostly always. While you are at it, using coding conventions so your code is uniform with others (improving readability and maintainability) is also a good idea. Class, method and property names should be written using UpperCamelCase, while variables and such should be written using normal camelCase.

    With that out of the way, let's take a look at your problem. Start by using Debug.Log() to check if your OnTriggerStay and the contained if statements actually get entered to see where the problem lies. https://docs.unity3d.com/ScriptReference/Debug.Log.html

    Edit: Also code is executed sequentially. Currently you do something like "if bool true, bool = false" and after that you do "if bool false, bool = true", which means the bool will always be true at the end of your OnTriggerStay, which is probably not intended and may already be your problem. If you want one or the other to happen, take a look at 'else if'.
     
  3. xaviza11

    xaviza11

    Joined:
    Jul 23, 2020
    Posts:
    3
    Ok, I try the debuglog and the script is now this, Its good?

    Code (CSharp):
    1. public class animador : MonoBehaviour
    2. {
    3.     Boolean  opendoor;
    4.  
    5.     private void OnTriggerStay(Collider other)
    6.     {
    7.         Debug.Log("inside");
    8.  
    9.         if (other.tag == "actmodulo") {
    10.             if (Input.GetKeyDown(KeyCode.Q))
    11.                forniture.GetComponent<Animation>().Play("modulo");
    12.             if (opendoor == true)
    13.                 opendoor = false;
    14.             else if
    15.                 (opendoor == true);
    16.                  
    17.  
    18.         }
    19.     }
    20.  
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.       opendoor = false;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.      
    32.     }
    33.  
    34.  
    35.     public GameObject forniture;
    36.  
    37.  
    38. }
     

    Attached Files: