- Home /
How to Sync bullet spawn with shoot animation
Hi, I have a question, how do I make the shoot animation sync with bullet spawn? right now when I'm clicking the fire button, it automatically fires the bullet but the animation is delayed by i think 0.2 secs. what I wanted to do is when I click the fire button, It will shoot the bullet exactly when my player raises his gun. Thanks in advance I really appreciate it.
here's my code that is attached to the CharacterController:
CharacterController controller;
public Animator animator;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
if (!controller)
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
if (weapon)
{
animator.SetTrigger ("Shoot");
weapon.GetComponent<WeaponShooter> ().Shoot ();
}
}
}
And This One is my WeaponShooter Code:
using UnityEngine;
using System.Collections;
public class WeaponShooter : MonoBehaviour {
public Rigidbody projectile;
public int ammo;
public Transform spawnPoint;
public int projectileForce;
// Use this for initialization
void Start () {
if (ammo <= 0)
{
ammo = 20;
}
if (projectileForce <= 0)
{
projectileForce = 3;
}
}
public void Shoot()
{
if (ammo > 0)
{
Rigidbody rb = Instantiate (projectile, spawnPoint.position, spawnPoint.rotation) as Rigidbody;
rb.AddRelativeForce (Vector3.forward * projectileForce, ForceMode.Impulse);
ammo--;
}
else
{
Debug.Log ("Reload");
}
}
// Update is called once per frame
void Update () {
}
}
Comment
Your answer
Follow this Question
Related Questions
Shooting & Animation Problem 1 Answer
Issues with bullet not subtracting properly 1 Answer
How to make 3 bullets fire at different angles 1 Answer
How to avoid speed change in bullets while moving? 1 Answer
Shooting script C# 1 Answer