- Home /
My animation is out of sync
I have an AI for throwing objects at the player with a random range between shots. I created an animation event for my AI's animation and called the event at a specific frame. When I do that the whole logic becomes redundant and throws the object relative to the frames. The animation has 8 frames. The function I called is Shoot. Below is the code. Any help is appreciated.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fire : MonoBehaviour
{
float shotTimer;
[SerializeField] float minTimeBetweenShots = 0.2f;
[SerializeField] float maxTimeBetweenShots = 3f;
public GameObject BallPrefab;
public GameObject[] Collectibles;
private int randomPrefabCollectibles;
GameObject BallPosition;
[SerializeField] float projectileSpeed;
[SerializeField] Transform spawnPoint;
void Start()
{
shotTimer = UnityEngine.Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
void Update()
{
CountdownAndShoot();
}
void CountdownAndShoot()
{
shotTimer -= Time.deltaTime;
if (shotTimer <= 0f)
{
Shoot();
shotTimer = UnityEngine.Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
}
void Shoot()
{
randomPrefabCollectibles = UnityEngine.Random.Range(0, 2);
GameObject Cannon = Instantiate(Collectibles[randomPrefabCollectibles], spawnPoint.position, Quaternion.identity) as GameObject;
Cannon.GetComponent<Rigidbody2D>().AddForce(transform.right * projectileSpeed, ForceMode2D.Impulse);
}
}
Comment
Your answer
Follow this Question
Related Questions
how to have a configurable joint model match a 3Dmesh animation 1 Answer
mecanim stuck 0 Answers
Unity Mechanim playing default layer state animation when gameObject is duplicated in the scene 0 Answers
How do i add an AnimationEvent to an animation of a single instance with script? 0 Answers
Trying to make an animation delay between each loop 2 Answers