How to press a 3D button with Mouse Click

I have a 3d button that looks like this and want to

image

and want to have the mouse click it and it animates by pushing( recessing in space) in 3d and back to original position after.

I created an animation file of it recessing when pushed but i cant seem to get the animation to play on trigger. Is there a script I could make to do a button click play an animation of it being pressed and returning to original position? all in 3d , no 2d buttons.

Generally you’ll need to use a raycast, theres a few different types and implementations depending on your needs.
Sorry for image im on my phone and couldnt be bothered typing it.
1_06e7JPn2Ex66U9C7XFk22Q

so how does someone use this script in unity to make the button animate , i applied it to the object but nothing happens…

using UnityEngine;
using System.Collections;

// Detects clicks from the mouse and prints a message
// depending on the click detected.

public class GetButtonDown : MonoBehaviour
{
    void Update()
    {
        Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if (Physics.Raycast(rayOrigin, out hitInfo))
        {
            Debug.Log("Hit: " + hitInfo.point);
        }
    }
}

You should wrap it in an if statement so it only fires on mouse click, I also wouldn’t put it on the button but technically it should still have worked. I usually put it on the camera.

The hitInfo is the object you’ve hit, so you can access the transform and any other components to call the animation method directly, or send a message or event for particular items by checking their tag for example, as it will probably get a null ref trying to access the component of anything else that is hit.

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;
        if (Physics.Raycast(rayOrigin, out hitInfo))
        {
            Debug.Log("Hit: " + hitInfo.transform);
            if (hitInfo.transform.tag == "StartButton")
            {
                hitInfo.transform.GetComponent<ButtonObj>().Animate();
                //hitInfo.transform.SendMessage("Animate");
            }
        }
    }
}

thanks for the code, but i tried it in the top main camera and nothing happens same with placing it on the text, maybe i got my code wrong on the top portion of the code , the second code produces errors

image


image
i enclosed the bracket for void between public class and void Update

unless im doing this wrong i still get error messages and the script doesn’t work even with the object button i want to push is renamed StartButton

The code works fine I think your implementation is a bit off, you were still missing the closing bracket in your GetMouseButtonDown class

Here is a basic setup for your script that you can attach to the camera, and another for the gameobject that you want to raycast. I’ve removed the check for the tag, it was just a recommendation.

Good luck.

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

public class GetMouseButtonDown : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(rayOrigin, out hitInfo))
            {
                hitInfo.transform.GetComponent<ButtonObj>().Animate();
            }
        }
    }
}

Script on the button gameobject:

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

public class ButtonObj : MonoBehaviour
{
    public void Animate()
    {
        //Animation logic goes here
        Debug.Log("Animate Logic Call");
    }
}

thanks you so much i will try this out !
image
image


you said place it on a button script right? i used physics button

image