- Home /
About Shooting With Gun
Hi.I made An animation for my gun called "shootwithm9" i have a bullet called "Bullet" and i wrote a script for shooting and im getting errors this is my script:
using UnityEngine;
using System.Collections;
public class M9 : MonoBehaviour {
public Transform M9bullet;
public Transform BulletSpawn;
public int OurAmmo = 100;
private RaycastHit hit;
private Vector3 dir;
private Quaternion rot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (animation.isplaying("shootwithm9") == false) {
if (Input.GetButtonDown ("Fire1")) {
if (OurAmmo > 0) {
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0));
if (Physics.Raycast(Ray, out hit)) {
dir = (hit.point - BulletSpawn.position).normalized;
}
else {
dir = ray.direction;
}
rot = Quaternion.FromToRotation (-M9bullet.transform.forward, dir);
Instantiate(M9bullet, BulletSpawn.position, rot);
Animation.Play ("shootwithm9");
audio.Play();
OurAmmo -= 1;
}
}
}
//CheckAmmo
}
}
Check The Script. Thank you and sorry for bad english
What's the error? The message tells a lot about where, and what the problem is if you can read it
Answer by JxWolfe · Jun 18, 2017 at 08:30 PM
Ok i looked at the code and came up with this
using UnityEngine;
using System.Collections;
//just to make sure the animation component is on the object
[RequireComponent(Animation)]
public class M9 : MonoBehaviour {
public Transform M9bullet;
public Transform BulletSpawn;
public int OurAmmo = 100;
private RaycastHit hit;
private Vector3 dir;
private Quaternion rot;
//problems found that i found ##############################################################33
Animation yourAnimation;
AudioSource yourAudio;
// Use this for initialization
void Start () {
yourAnimation = GetComponent<Animation> ();
}
// Update is called once per frame
void Update () {
if (yourAnimation.IsPlaying("shootwithm9") == false) {
if (Input.GetButtonDown ("Fire1")) {
if (OurAmmo > 0) {
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0));
if (Physics.Raycast(Ray, out hit)) {
dir = (hit.point - BulletSpawn.position).normalized;
}
else {
dir = ray.direction;
}
rot = Quaternion.FromToRotation (-M9bullet.transform.forward, dir);
Instantiate(M9bullet, BulletSpawn.position, rot);
yourAnimation.Play ("shootwithm9");
yourAudio.Play();
OurAmmo -= 1;
}
}
}
//CheckAmmo
}
}
I think it should work, and if i doesn't then post the new errors and i will take another look.
Thank You It's Very Nice. But We Have 3 Errors With This line:
[RequireComponent(Animation)]
These Are Errors: 
sorry for my questions I'm new
just get rid of it, i am new to requiring components, and it would only add the animation component if it wasn't on the object
Your answer
Follow this Question
Related Questions
Preventing all animations from playing? 0 Answers
Character won't play animation 8 Answers
Animation Play Problem (Att: Video Example) 1 Answer
animation play in game over 1 Answer
