- Home /
Bow Scripting, releasing the string
Hey, I just found an old bow script from Seth Bergman and I added some code for the animation clips and tried it on my bow model. There are some problems , however. When I hold my mouse button to fire, it doesn't wait for me to release it , it just shoot. Then , the bow is stuck in the aim animation unless I click again, glitching it out for good, jumping between animations at fast speed for no apparent reason D: I commented out the arrow part since I don't need it at the moment but here is the code
var arrowPrefab: Rigidbody;
var ArrowSpeed = 100.0;
var fireRate = 1.5;
var nextFire = 0.0;
var pullStartTime = 0.0;
var pullTime = 0.5;
var falsePull : boolean;
var maxStrengthPullTime = 1.5;
var fullAnim: AnimationClip;
var myAnimation : Animation;
function Start(){
falsePull = false;
myAnimation=GetComponent.<Animation>();
myAnimation.AddClip(fullAnim,"Longbow_aim",0,38,true);
myAnimation.AddClip(fullAnim,"Longbow_fire",38,47,true);
}
function Update ()
{
// pull back string
if(Input.GetButtonDown("Fire1"))
{
if(Time.time > nextFire)
{
myAnimation.Play("Longbow_aim");
pullStartTime = Time.time;
}
else{
falsePull = true;
}
}
// fire arrow
if(Input.GetButtonUp("Fire1")){
if(!falsePull)
{
nextFire = Time.time + pullTime;
myAnimation.Play("Longbow_fire");
var timePulledBack = Time.time - pullStartTime;
if(timePulledBack > maxStrengthPullTime)
timePulledBack = maxStrengthPullTime;
var arrowSpeed = ArrowSpeed * timePulledBack;
/*
var arrow : Rigidbody = Instantiate(arrowPrefab,
GameObject.Find("FIREPOINT").transform.position, transform.rotation);
Physics.IgnoreCollision(arrowPrefab.collider, transform.root.collider);
arrow.rigidbody.AddForce(transform.forward * arrowSpeed);
*/
}
else
falsePull = false;
}
if (myAnimation.isPlaying == false)
{
animation.CrossFade("PLAYER_LONGBOW_idle");
}
}
Answer by Zeclown · Sep 19, 2014 at 10:32 PM
I've solved my initial problem by wrapping the clips in clamped mode! now I can't get to fire the arrow though! here is the code i'm using
var falsePull : boolean;
var maxStrengthPullTime = 1.5;
var ammo:int;
function Start(){
falsePull = false;
}
function Update ()
{
//debug
if(animation.IsPlaying("PLAYER_LONGBOW_fire"))
print("fire");
if(animation.IsPlaying("PLAYER_LONGBOW_pull"))
print("fire2");
// pull back string
if(Input.GetButtonDown("Fire1"))
{
if(Time.time > nextFire)
{
animation.Play("PLAYER_LONGBOW_pull");
pullStartTime = Time.time;
}
else{
falsePull = true;
}
}
// fire arrow
if(Input.GetButtonUp("Fire1"))
{
if(!falsePull)
{
nextFire = Time.time + pullTime;
animation.Play("PLAYER_LONGBOW_fire");
var timePulledBack = Time.time - pullStartTime; // this is how long the button was held
if(timePulledBack > maxStrengthPullTime) // this says max strength is reached
timePulledBack = maxStrengthPullTime; // max strength is ArrowSpeed * maxStrengthPullTime
var arrowSpeed = ArrowSpeed * timePulledBack; // adjust speed directly using pullback time
var arrow : Rigidbody = Instantiate(arrowPrefab,
GameObject.Find("FIREPOINT").transform.position, transform.rotation);
Physics.IgnoreCollision(arrowPrefab.collider, transform.root.collider);
arrow.rigidbody.AddForce(transform.forward * arrowSpeed); // adjusted speed
ammo -= 1;
if(ammo >= 0)
{
animation.Play("ArrowKnocking");
}
}
else
falsePull = false;
}
}
The arrow i'm using is a child of the bow and has a rigidbody. However, it stays on the bow even though i'm trying to send it forward at line 56 using the arrow.rigidbody.AddForce.Any idea why?
Your answer
![](https://koobas.hobune.stream/wayback/20220613163212im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
My distance variable is not changing? 2 Answers
Rest postion after animation plays 0 Answers
Why doesn't my script work? 1 Answer