- Home /
Quick code help! (c#)
Im trying to have it so when the object collides with tagged object it changes to "isBlocked" but still is able to change back to the others.
using UnityEngine;
using System.Collections;
public class Chase : MonoBehaviour {
public Transform player;
static Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction,this.transform.forward);
if(Vector3.Distance(player.position, this.transform.position) < 15 && angle < 90)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 3)
{
this.transform.Translate(0,0,0.05f);
anim.SetBool("isWalking",true);
anim.SetBool("isAttacking",false);
}
else
{
anim.SetBool("isAttacking",true);
anim.SetBool("isWalking",false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
}
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "OhNo")
{
anim.SetBool ("isBlocked", true);
}
}
}
It keeps repeating "isBlocked". $$anonymous$$ost of the time it will not even register it.
You're never setting isBlocked back to false, which will be part of your problem. Try doing that in OnTriggerExit
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "OhNo")
{
anim.SetBool ("isBlocked", true);
}
}
void OnTriggerExit (Collider col)
{
if (col.gameObject.tag == "OhNo")
{
anim.SetBool ("isBlocked", false);
}
}
Answer by a161803398874 · Aug 03, 2017 at 05:22 AM
I think your problem is that when you go to the isBlocked animation its stays there and you want to transition to the other animations.... if thats so,,,, then you need to add the proper transitions in the animation window using the isBlocked parameter between the "Blocked" animation and some other... and maybe add a timer in wich the player gets stuck and then it can move again.... for this you will add a "courutine" in the "if" statement that compares the tag "OhNo".... In the Courutine set the parameter to isBlocked=True for 5 seconds and then isBlocked=False...
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "OhNo")
{
StartCoroutine (BlockedCourutine());
}
}
public float blockDuration = 6f;
private IEnumerator BlockedCourutine()
{
anim.SetBool ("isBlocked", true);
yield return blockDuration;
anim.SetBool ("isBlocked", false);
}
Answer by Timo326 · Aug 02, 2017 at 11:32 PM
anim.SetBool ("isBlocked", !anim.GetBool("isBlocked"));
Is that what you mean?