Hi, this seems like the easiest question to ask here but all the searching on forums and YouTube turns up either way too complicated or outdated solutions.
I have an FBX object with embedded animation of a car that drives in a circle. I’ve loaded the object and attached the animation to the object and it drives exactly how it does in the 3d package. Perfect.
But… how do I get the animation to only start when I click on the object or (even better) a button?
I’ve even created another node in the Animator between the “Entry” node and “Base Stack” (Animation file in the FBX file) node as I read that you can do that and use that node to start the animation.
My strength is 3D, not coding so “even an idiot can follow this” steps would be great!!
This will trigger the animation to play when the Left Mouse button is pressed.
If you want to activate the animation by clicking on the object, it’s similar but you will need to look into Raycasting - Unity - Scripting API: Physics.Raycast
Take a look at the attached screenshot of the Animator controller for help.
Could you please help me? I’m in unity 3d, and I’m trying to open up fridge doors. There are 2. I want to left click to open it, and left click again to close it. I’m using C#, and my Unity 3D Version is 2018.1.0f2. I have 2 animations, too; an OPEN and a CLOSE animation.
@wyldelionel - Make sure you’re using a fresh C# script. Put everything inside the curly braces of the class, and replace the existing Start() and Update() methods. Other than that, we’d need to see your script.
I followed the steps and copied the code correctly but I get this error:
Assets\On_Click.cs(4,10): error CS0116: A namespace cannot directly contain members such as fields or methods
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Animator anim;
public class On_Click : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
anim = gameObject.GetComponent();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
anim.SetTrigger(“Active”);
}
}
}
Everything that you code other than ‘using UnityEngine…’ etc must be inside the class itself. Your issue is that your anim variable is outside of the class, which will most certainly cause some issues. Just move the variable inside of the MonoBehaviour class and you’ll be good to go!
This would have been a great way for my animation to play when I click but sadly this does not work in unity 5 , please fix this soon, I know this may be a problem for some people:smile:
The code snippet you attached does not work. This is what the error said:
Assets\playanim.cs(8,13): error CS0116: A namespace cannot directly contain numbers such as fields or methods
Assets\playanim.cs(8,20): error CS0246: The type or namespace name “Animator” could not be found (are you missing a using directive or an assembly refrence?)
Please follow up on this, I really need this. Thanks!
Hi, I Used this script and it works great. But can someone please help with how to introduce raycasting into this script so that the chest will open only when clicked on?
Thanks a lot!
I’m new to coding and i needed help so i went here. I copied everything that i saw on the forum and it just won’t work! I keep getting this error saying “error Cs1003: syntax error, ‘,’ expected.” I tried putting in the commas but it just wont work. Need help on this.
Hi all, I am trying to play door opening animation by clicking on a door, there are basically 4 doors in my scene, but when I play the scene all 4 doors animate simultaneously without clicking, for clicking functionality I even tried attaching a button to each door but it didn’t work. Any help would be appreciated. Thanks!
I am attaching a code for door animation too:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorRotation : MonoBehaviour
{
public GameObject doorRotation;
void Update()
{
if(Input.GetKey(KeyCode.Mouse0))
{
doorRotation.GetComponent<Animator>().Play("Rotate");
}
}
}