Home > OS >  trying to make a button so if the player steps on it or places a cube on it, it presses down and ope
trying to make a button so if the player steps on it or places a cube on it, it presses down and ope

Time:12-23

Trying to make a button so if the player steps on it or places a cube on it, it presses down and opens a door. I can't get past the part of it just going down. Every time I step on it, it teleports way off into the distance. I also can't find anything a event systems at how I can open a door with a button.

using UnityEngine;

using UnityEngine.EventSystems;

using System.Collections;

public class DoorOpener : MonoBehaviour
{

    bool pres = false;
    public GameObject cube;
    public GameObject player;
    private float x = 49.849998474121097f;
    private float y = 0.03762388229370117f;
    private float z = 121.22000122070313f;
    private float ny = -0.0123761177f;
    void OnCollisionEnter(Collision collision)
    {
        bool press = true;
        if (collision.gameObject == player)
        {
            transform.position = new Vector3(x, ny, z);
        }
        if (collision.gameObject == cube)
        {
            transform.position = new Vector3(x, ny, z);
        }
    }

    private void Update()
    {
        if (pres)
        {
            transform.position = new Vector3(x,  ny, z);
        }
    }

}

CodePudding user response:

buddy! I examined your code. First of all, I suggest that you should change OnCollisionEnter void. OnCollisionEnter works only when an object touches the door button. But if you write OnCollisionStay, it will work as long as it stays in contact and instead of "press boolean" you can use OnCollisionExit. In addition, you can write if (collision.gameObject.CompareTag(player)) instead of using GameObject. In this method, you can just give the tag "player" to your player. This is just a suggestion not so important but I believe that it will be better. Anyway, you said that my button is teleporting. It is completely normal because your code (transform.position = new Vector3(x, ny, z);) is not an animation. It's for teleport objects. You can make a basic animation in Unity for button movements. and then control the animation translations with bool parameters in Unity Animator. Look at my example code. According to this code, when an object, which has a "block" tag touches the button, the animator parameter "isButtonPressed" will be true. When this object stops touching our button, the parameter change with false and the button up animation will play.

    public class buttonscripts : MonoBehaviour
{
    Animator _animator;

    void Start()
    {
        _animator = GetComponent<Animator>();
    }

    void OnCollisionStay(Collision collision)
    {
        if(collision.gameObject.CompareTag("block"))
        {
            _animator.SetBool("isButtonPressed", true);
        }
    }

    void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.CompareTag("block"))
        {
            _animator.SetBool("isButtonPressed", false);
        }
    }

}

You can see my animator in here

CodePudding user response:

First of all you are creating a new local variable called press within the OnCollisionEnter method. So your Update will never take effect at all.

Then instead of using hardcoded positions - which is probably where your unexpected teleport comes from (have in mind that the Inspector shows the local position but you set the global one) - I would rather use something like e.g.

// Configure via the Inspector
public Vector3 moveOffsetOnPressed = Vector3.down * 0.04f;

private Vector3 originalPosition;
private Vector3 pressedPosition;

void Start()
{
    originalPosition = transform.position;
    pressedPosition = originalPosition   moveOffsetOnPressed;
}

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject == player || collision.gameObject == cube)
    {
        transform.position = pressedPosition;
    }
}

void OnCollisionExit(Collision collision)
{
    if (collision.gameObject == player || collision.gameObject == cube)
    {
        transform.position = originalPosition;
    }
}

Note though: There might still be an issue now! What if by moving the button down .. well, it is not colliding with the player/cube anymore => you move it up again => You might fire the player/cube away into outer space ;)

What could you do about that? Only move it up again if the button stays without a collision for too long time (e.g. 0.5 seconds) like

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject == player || collision.gameObject == cube)
    {
        StopAllCoroutines ();
        transform.position = pressedPosition;
    }
}

void OnCollisionExit(Collision collision)
{
    if (collision.gameObject == player || collision.gameObject == cube)
    {
        StopAllCoroutines ();
        StartCorouine (ExitRoutine ());
    }
}

private IEnumerator ExitRoutine ()
{
    yield return new WaitForSeconds (0.5f);

    yield return new WaitForFixedUpdate ();

    transform.position = originalPosition;
}
  • Related