- Home /
Another animation is not working
Greetings,
Here is my code -
using UnityEngine;
using System.Collections;
public class enemyai : MonoBehaviour {
public Transform target;
NavMeshAgent agent;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
agent.SetDestination(target.position);
GetComponent<Animation>().Play ("Run");
if(Input.GetKeyDown(KeyCode.LeftAlt))
{
GetComponent<Animation>().Play("Attack_01");
}
}
void OnTriggerEnter(Collider hel)
{
if(hel.gameObject.tag == "enemy")
{
GetComponent<Animation>().Stop("Run");
}
}
}
It's not playing 2nd animation cause it's not stopping 1st , if i press left alt then it just play it for i millisecond , just a little bit and then again play run animation as loop .
can you please tell me how can i correct this in unity 5.1 , it have GetComponent so a bit confusing for me , can you write that code line to fix this , thanks .
just got that , this is the final code , hope it helps you -
using UnityEngine;
using System.Collections;
public class enemyai : $$anonymous$$onoBehaviour {
public Transform target;
Nav$$anonymous$$eshAgent agent;
public bool attack=false;
// Use this for initialization
void Start () {
agent = GetComponent<Nav$$anonymous$$eshAgent>();
}
// Update is called once per frame
void Update () {
agent.SetDestination(target.position);
if(attack==false)
{
GetComponent<Animation>().Play ("Run");
}
if(attack==true)
{
GetComponent<Animation>().Play("Attack_01");
}
}
/* GetComponent<Animation>().Play ("Run");
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftAlt))
{
GetComponent<Animation>().Play("Attack_01");
}
}*/
void OnTriggerStay(Collider stay)
{
if(stay.gameObject.tag == "areap")
{
attack=true;
}
}
void OnTriggerExit(Collider exit)
{
if(exit.gameObject.tag == "areap")
{
attack = false;
}
}
}
If your second script works then why not explain what you changed and post as a answer? (you can use convert to answer on your comment)
Well @Shark Boy that's good idea, but this are not program$$anonymous$$g noob forums ,where i need to explain each line , you can read then and according to logic , you can simplify that easily .
Yes but changing to a answer cleans up the unanswered questions and makes it so other people can search answers and have this come up. With a solution. No you do not need to go into line by line explanation of what you did I just meant say what was wrong and point out what you changed.
@Shark Boy , Yeah sure -
OnTriggerEnter() Function called 1 times in 1 frame and ends up with one call , so animation plays just for 0.02 sec . So animation plays a bit and again play the old one where it starts from , So i just changed it with OnTriggerStay() to play until it's in trigger .Now i delete left alt key command to run animation cause i want to make it Enemy ai , and take one bool to show that if it triggers (I take bool becuase trigger will not run with update function , you know what i meant to say) and then attack is true , then update calls - check attacks true and play attack ins$$anonymous$$d of run and when Triggers exit , attack goes false and then again it will play run , until it will not found trigger .
Your answer
Follow this Question
Related Questions
Where i use Getcomponent() here in this script 2 Answers
Choppy Game Screen 3 Answers
How do I acces a specific GameObject within the terrain???? 0 Answers
Unity stops while I work with my project 0 Answers
HasComponent 4 Answers