- Home /
Beginner gun script, 4.0 woe, assistance sought
The problem is not the script,the script works fine the problem is that in unity 4.0 the function
animation.Play("walking")
isnt working i dont know what to do for that so if someone knows plz help me because have stuck at this problem for almost 3 months and i can do nothing!!
here is the script if anyone knows hot to make the animation to play in unity 4.0 plz help me
var Clips : int = 20;
var Range : float = 900;
var Force : float = 900;
var BulletsInClip : int = 27;
var RelodeTime : float = 4;
var BulletsLeft : int = 0;
var ShootTimer : float = 0;
var ShootCooler : float =0.9;
var RelodingAudio : AudioClip;
var ShootingAudio : AudioClip;
var ShootingAnimation : Animation;
var ReloadingAnimation : Animation;
var ReloadingString : String;
var ShootingString : String;
function Start ()
{
BulletsLeft = BulletsInClip;
}
function Update ()
{
if (ShootTimer > 0)
{
ShootTimer -= Time.deltaTime;
}
if (ShootTimer < 0)
{
ShootTimer = 0;
}
if (Input.GetMouseButton(0)&& BulletsLeft > 0)
{
if(BulletsLeft == 0)
{
Relode();
}
if (ShootTimer == 0)
{
Shooter();
ShootTimer = ShootCooler;
}
}
if (Input.GetKeyDown(KeyCode.R))
{
Relode();
}
}
function Shooter ()
{
ShootingAnimation.Play(ShootingString);
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, DirectionRay,Hit,Range))
{
if (Hit.rigidbody)
{
Hit.rigidbody.AddForceAtPosition(DirectionRay* Force, Hit.point);
}
}
BulletsLeft --;
PlayShootAudio();
if(BulletsLeft < 0)
{
BullletsLeft =0;
}
if(BulletsLeft == 0)
{
Relode();
}
}
function Relode ()
{
PlayRelodeAudio();
ReloadingAnimation.Play(ReloadingString);
yield WaitForSeconds(RelodeTime);
if(Clips > 0)
{
BulletsLeft = BulletsInClip; Clips --;
}
}
function PlayShootAudio ()
{
audio.PlayOneShot(ShootingAudio);
}
function PlayRelodeAudio ()
{
audio.PlayOneShot(RelodingAudio);
}
Edit your question title. Edit the code, this is unreadable. And add builder error message.
Please format your code correctly on future posts. Fixed it for you this time.
You have already accepted an answer, so unless Piflik can help, am not sure you'll get new people reading this. If you cannot undo the accepted answer, you may have to ask a new question for more traffic. $$anonymous$$ake sure you link back to this question (to avoid duplicate question confusion. I don't know, but this may be the best way for you to get an answer =]
Edit : I cannot see anywhere in your code the line animation.Play("walking")
Answer by Piflik · Dec 30, 2012 at 05:34 PM
Without knowing what exactly does not work, there is only a limited amount of insight I can give you.
First, (BulletsLeft == 0) is never true inside if(BulletsLeft > 0). Even if you pull it below the shoot part, where BulletsLeft is decremented it is still redundant, since you reload as part of the shooter function.
if(BulletsLeft < 0) inside the shooter function is redundant, since it is only called when you have at least one bullet left.