cant play an animation when pressing a button - c#

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 :)

Related

Animate an object by pressing Key / or Mouse Click Unity3D

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.

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()
{
}

How can i make in the blend tree that the player will keep walking if he is also aiming with the weapon?

Now when the player is aiming he can't walk. I need to make it stop aiming to be able to keep walking. I'm using the mouse right button for aiming with aiming parameter false/true.
And i created a blend tree there i mixed between Walk and Idle.
But now i want also to mix between Walk and Aiming.
And when i make double click on Movement there the blend tree:
So now i can walk and when i stop walking with the keys it's idling.
I can also use the mouse right button for aiming one click on the mouse right button will keep aiming another click will stop aiming.
But when i'm in the aiming state i can't walk.
The question is how do i mix the Walk with the Aiming (The aiming animation i have is called: Rifle_Aiming_Idle there is also animation called Rifle_Aiming_Walk_F) ?
This animations are not my own. But i wonder if i should mix the aiming_Idle with the Walk(HumanoidWalk i have already in the blend tree or to use somehow only with the Rifle_Aiming_Walk_F somehow).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Soldier : MonoBehaviour
{
private Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
var inputVelx = Input.GetAxis("Horizontal");
var inputVely = Input.GetAxis("Vertical");
transform.Rotate(0, inputVelx, 0);
anim.SetFloat("VelX", inputVelx);
anim.SetFloat("VelY", inputVely);
if (Input.GetMouseButtonDown(1))
{
anim.SetBool("Aiming", !anim.GetBool("Aiming"));
}
}
}
There is a short video clip in youtube the owner of this package he made showing the movements too of the aiming/s. I'm not sure just how to make that the character will walk and aiming at the same time:
Sci Fi Space Soldier - PolygonR
You'll need to create multiple Layers in the animator controller.
Then add Avatar mask. Please take a look at here:
http://www.theappguruz.com/blog/how-to-animate-specific-body-parts-using-avatar-mask-in-unity
As a solution you can duplicate your shoot animation and rename it as shoot_walking then animate it.
Also check it:
https://docs.unity3d.com/ScriptReference/AnimationState.AddMixingTransform.html

Categories