Animate an object by pressing Key / or Mouse Click Unity3D - c#

currently i'm trying to animate an object by clicking or pressing a key. Unfortunately, script won't work and I have tried soooooo many other ways to do it.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnBoard : MonoBehaviour
{
public Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void OnTriggerStay(Collider player) {
if (player.tag == "Player" && Input.GetKeyDown(KeyCode.G)){
//Debug.Log("touchyy");
anim.SetTrigger("turn");
}
}}
I have assigned this script file to object itself. Object has a box collider. Animation has a trigger named "turn". When player enters to the collider zone, I want player to be able to activate animation of the object with a click/or keypress.
I do get "Debug.Log" when player enters the zone. So I believe that there is no problem in detecting the collision. But just can't manage to animate object in any way.
Any help? Thank you!

OnTriggerStay is run like FixedUpdate (since it involves physics), so none of the Input events will work correctly inside it for the same reason that they won't work right in FixedUpdate. All Input functions must only be used in Update method.

Related

cant play an animation when pressing a button

screen shot of the animator panel
so as you can see I am trying to play an animation called Attack when triggering the parameter Attack but whenever I press the triggering button that I chose the animation just doesn't work and the trigger does not get activated
here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class attack : MonoBehaviour
{
public Animator animator;
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
Attack();
}
}
void Attack()
{
animator.SetTrigger("Attack");
}
}
by the way, I am trying to follow this guy's video
https://youtu.be/sPiVz1k-fEs
Suggestions:
Make sure you have assigned this script to a game object.
Assign the right animator that you want to animate in inspector of this script.
Check if you have added 'Attack' parameter in that animator and the spelling is same like the one mentioned as parameter in your code.
and lastly check if you have set the trigger for the transition of animation in the animator( in animator its the arrow that is pointing towards your animation ).
Let me know if you don't understand any of this I'll breifly explain.
Hopefully that will help... Happy coding :)

Enable/ Control script on game object from animator controller in unity?

I want to create an analog glitch effect in unity for may main camera when my game character/ camera looks at a certain object.
I have seen it in this youtube video, but the part I am struggling with was not explained.
I am using this Glitch .unitypackage from Github (link).
I attached the AnalogGlitch Script from the package to my main camera game object, but this will apply the glitch effect the entire time.
Instead I want to enable the glitch effect when my character/ camera faces this certain object. I have a working animator controller, which enters a "facing state" when my camera looks at the object and a "Not facing state" when looking somewhere else (as seen in the next image).
Ideally I would just enable/ disable the AnalogGlitch.cs Script from my main camera game objects when this animation starts. I tried setting up a EnableGlitch.cs script for this, but it will not let me reference any game object in my scene. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnableGlitch : StateMachineBehaviour
{
public GameObject maincamera;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
//maincamera.GetComponent<AnalogGlitch>().enabled = true;
}
}
Also is there a better way to control the AnalogGlitch.cs script from the animator controller? I would also like to control the variables within this script to modify the effect while in this animation state.

Why onmousedown is not working on empty gameobject?

I added a box collider to the gameobject.
And yet the onmousedown in the script never trigger.
And the script Back
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Back : MonoBehaviour
{
public GameObject[] objectstexts;
public Rotate rotate;
private void OnMouseDown()
{
rotate.StartRotations();
}
}
The gameobject back is not a cube it's not the cube I want to click on.
I'm using the Back empty gameobject just to get it's name for switching the name on the bottom cube. From Quit to Back.
The back empty gameobject is positioned at the same place of the Quit cube.
Make sure the script is attached to the game object you want to be clickable.
Make sure you have an enabled collider
Make sure you have not resized or changed the position of the collider
Make sure there are no other colliders in the way. OnMouseDown() works just like a Raycast()...the first thing hit is what gets the OnMouseDown().
If everything looks okay and it still does not work, delete the object and start over by adding the new object, adding the script...
The very first statement on the API of OnMouseDown says
OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
Your empty GameObject didn't have any those.
You could for example add a BoxCollider to it, though the usability of an invisible button is very questionable. The main issue now might however be that the other cube is in front of this collider so you never actually hit it.
It is blocked by the other cube

I would like two objects to play their separate animations when the user clicks on them in Unity using C#

I am new to coding, so please go easy and try to make this as simple as possible. I am surprised that what I am trying to do appears to be so difficult.
I have two objects, A and B. They each have a different animation, 1 and 2. I want the user to click on A and watch animation 1, or to click on B and watch animation 2. I also want each object to play their relevant animations and then change to different scenes.
Can this be done just in the Animator window without any code using bools or triggers and transitions? (I have tried multiple ways and can't work it out.)
If it needs a code attached to objects A and B, I have a trigger animation script (attached below) but it only plays one of the animations and is just if the mouse button is clicked, not by clicking on object A or B.
I can successfully do what I need with a change scene script: objects A and B have the change scene script attached and two different scenes to change to. How is the animation I want to play different and seemingly so complicated to implement? I haven't needed to declare any gameobjects in that script. Perhaps I need to GetComponent and something to do with PlayAnimation, but I have searched and searched and am surprised no one else has had this issue, or perhaps I'm not asking in the correct way. There are many tutorials that tell you how to play one animation, but not multiple.
using UnityEngine;
using System.Collections;
/// <summary>
/// The TriggerAnimation class activates a transition whenever the Cardboard button is pressed (or the screen touched).
/// </summary>
public class TriggerAnimation : MonoBehaviour
{
[Tooltip("The Animator component on this gameobject")]
public Animator animator;
[Tooltip("The name of the Animator trigger parameter")]
public string triggerName;
void Update()
{
// If the player pressed the cardboard button (or touched the screen), set the trigger parameter to active (until it has been used in a transition)
if (Input.GetMouseButtonDown(0))
{
animator.SetTrigger(triggerName);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ChangeScene : MonoBehaviour
{
public void GoToScene(string sceneName)
{
Debug.Log("I'm going to change scenes!");
SceneManager.LoadScene(sceneName);
}
}
Thank you so much in advance. I'm really stuck. I am trying to learn by following tutorials and using Sololearn, but I'm finding it very difficult to translate what I'm learning into things I want to do.
Here's the updated code:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
/// <summary>
/// The TriggerAnimation class activates a transition whenever the Cardboard button is pressed (or the screen touched).
/// </summary>
public class TriggerAnimation : MonoBehaviour
{
public Animator animatorVR;
public Animator animatorAR;
public void OnPointerClick(PointerEventData pointerEventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
void Update()
{
// If the player pressed the cardboard button (or touched the screen), set the trigger parameter to active (until it has been used in a transition)
//OnPointerClick;
{
animatorVR.SetTrigger(onPressVR);
animatorAR.SetTrigger(onPressAR);
}
}
}
I know this doesn't work but am not sure where to go from here.
Detecting a click on an object
Input.GetMouseButtonDown only checks the current state of the button, without any respect as to where the mouse is pointing. You're executing it within the Update event method, which runs every single frame.
The easiest way to detect if you're clicking an object is to put a Collider component (of any shape) on the object and use the OnMouseDown event function.
As noted in the comments by Galandil, OnMouse methods shouldn't be used for touch controls.
Running animations for two different objects
The Animator component can control a tree of objects (the object with the component, but also its children). You can do what you want without any additional code if you put objects A and B under a common parent object with Animator. Otherwise, yeah, you'll have to reference the other object somehow, e.g put something like this on both objects:
[Tooltip("The Animator component on this gameobject")]
public Animator animator;
[Tooltip("The Animator component on the other gameobject")]
public Animator otherAnimator;
...
animator.SetTrigger(triggerName);
otherAnimator.SetTrigger(triggerName);

Unity 2D Animation Won't Play after SetActive(true)

I have a 2D single screen style game where I have a disabled gameobject. Under this are several children that that are animated. The parent object holds the animator for the children. When I enabled the parent, the animations will not play. The animator looks as thought it's trying to processes the default entry state but does not complete. I've done some research on this and it seems that disabling and then enabling something does not start the animator. I've tried writing some code for it with no luck and setting the sprite renderer false won't work either as there are simply too many sprites. Here's my last attempt: Any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animationState : MonoBehaviour
{
public Animator anim;
private void OnEnable()
{
if (anim.gameObject.activeSelf)
{
anim.Play("pinWheelAnimation");
}
}
}
Unity doesn't work like that, You can't set an animator to the parent object that is going to influence animations from children, you are going to have to set up an animator for every child. If you want to modify animators of children from a parent object you can do that with a script that searches for all children and their components and changes their parameters.
Take a look at this: Animator parameters
Try to set an IdleState as DefaultState with no exit time and no animation and define a trigger event to go over to your animation state. Then activate the trigger in
void Start()
{
}

Categories