- Home /
Animations and animator layers.
So i've done this soooo many times yet it dont work. I've changed the code and tried in so many different ways for about 5 hours now. And it's kinda awkward because this is just so easy normally and such a small part of my script. But heres the problem:
I have one animator layer with a running and a idle animation with a bool which is run. Simple. Then i have an other layer with a higher priority with animations like casting a spell and other actions, because i need to be able to cast a spell when running.
Im trying to instantiate an object 0.8 seconds after i start the animation because it fints right in there. And i need to have a cooldown which is easy too. But somehow it still dont work. And because i have it on GetKey i need to set the animation to false after i dont cast a spell. But if i just hold down "N" to cast a spell the animation is not synced at all with the actual casting of the spell and the animations are super glitched. Im pretty sure it has something to do with the animator layers. Any help is welcome!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float fireRate;
public float force;
public GameObject[] Particles;
Animator anim;
float nextFire;
void Start ()
{
anim = GetComponent<Animator> ();
}
void Update ()
{
if (Input.GetKey (KeyCode.N) && Time.time > nextFire) {
nextFire = Time.time + fireRate;
anim.SetInteger ("Index", 1);
GameObject magic = Instantiate (Particles [0]) as GameObject;
magic.transform.position = transform.position + new Vector3 (0, 1.5f, 0);
magic.transform.eulerAngles = transform.eulerAngles + new Vector3 (0, 180, 0);
magic.GetComponent<Rigidbody> ().velocity = transform.forward * force * Time.deltaTime;
}
else if (!Input.GetKey (KeyCode.N))
{
anim.SetInteger ("Index", 0);
Debug.Log ("NOT HOLDING DOWN ATTACK");
}
}
}
Why don't you use Animation Events ins$$anonymous$$d of manually trying to find the timestamp in the animation?
Answer by fluxhackspro · May 07, 2018 at 04:00 PM
Thank you @TanselAltinel for the correct answer! I used an animation event on my actual animation and set up a custom function to controll the animation and movement glitches!
Your answer
Follow this Question
Related Questions
Animation playing backwards in a layer 0 Answers
UFPS - Player Animation Setup 0 Answers
How to control an animation with both GetButton and GetButtonDown 0 Answers
Load Animator From XML without using UnityEditor 0 Answers
How to rotate an object with a script after an animation has rotated it? 1 Answer