- Home /
Question by
alex8246 · Jun 22, 2019 at 02:35 AM ·
animationmecanimtransition
Mecanim transition between animation help
Hello, I have two animations, Rifle Idle and Firing Rifle, that seem to be not transitioning properly. Video here:
The transition to and from Walk Forward to Firing Rifle seems ok. Here is the code:
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public float range = 100f;
public AudioSource gunAudio;
public AudioClip gunSound;
float timer;
Ray shootRay = new Ray();
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
Light gunLight;
float effectsDisplayTime = 0.2f;
Animator anim;
bool isFiring;
void Awake ()
{
shootableMask = LayerMask.GetMask ("Shootable");
gunParticles = GetComponent<ParticleSystem> ();
gunLine = GetComponent <LineRenderer> ();
gunAudio = GetComponent<AudioSource> ();
gunLight = GetComponent<Light> ();
}
void Start ()
{
anim = gameObject.GetComponent<Animator> ();
}
void Update ()
{
timer += Time.deltaTime;
if (Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) {
isFiring = true;
Shoot ();
anim.SetBool ("isFiring", isFiring);
} else {
isFiring = false;
anim.SetBool ("isFiring", isFiring);
}
if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}
public void DisableEffects ()
{
gunLine.enabled = false;
//gunLight.enabled = false;
}
void Shoot ()
{
timer = 0f;
gunAudio.clip = gunSound;
gunAudio.Play ();
//gunLight.enabled = true;
gunParticles.Stop ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
I have attempted to edit the settings between transitions (checking and unchecking "has exit time", etc) but no luck as of yet. Any assistance would be greatly appreciated!
Oh and don't mind the raycast coming from the character's feet, that will be adjusted later haha
Comment
Your answer
Follow this Question
Related Questions
Nicer Mecanim transitions? 1 Answer
How to fluently transition into an animation state? 0 Answers
mecanim trasitions auto reset 0 Answers
Make mecanim transition after a delay in Unity3D 0 Answers
Interrupting animations 1 Answer