Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zeclown · Sep 17, 2014 at 04:48 PM · bow

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");
         }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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

Getting error: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges