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 ryno9788 · Sep 17, 2012 at 01:57 AM · javascriptshootingweapon

bow and arrow (load and fire)

I've been working on a bow for a fps shooter type deal. It works off the idea of getting mouse button down to pull the string back, and releasing the arrow with mouse button up (sorry it's really messy, just playing around with it). What it needs to do is, if release early drop or shoots very short, (maybe divide arrow speed by four) but how to I write this where I can get it to do this

 var arrowPrefab: Rigidbody;
 
 var ArrowSpeed = 100.0;
 var fireRate = 1.5;
 var nextFire = 0.0;
 var tIME = 0.0;
 var pulltime = 0.5;
 
 function Update ()
 {
 tIME = Time.time; 

// pull back string

 if(Input.GetMouseButtonDown(0)&& Time.time > nextFire)
 {
 nextFire = Time.time + fireRate;
 animation.Play("PULLBACK");
 
 }

// fire arrow

 if(Input.GetMouseButtonUp(0)&& Time.time >= nextFire)
 {
 
 nextFire = Time.time + pulltime;
  animation.Play("FIRE");
  
 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);


 }
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
1
Best Answer

Answer by Seth-Bergman · Sep 17, 2012 at 04:46 AM

here's how I would do it:

 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; // how long to hold button until max strength reached

 function Start(){
 falsePull = false;
 }

 function Update ()
 {
 
 // pull back string
 
 if(Input.GetMouseButtonDown(0))
 {
 if(Time.time > nextFire)
 {
 nextFire = Time.time + fireRate; // this line is unnecessary, since you are going to change it onMouseUp
 animation.Play("PULLBACK");
 pullStartTime = Time.time; //store the start time
 }
 else
 falsePull = true; 
 }
 
 // fire arrow
 
 if(Input.GetMouseButtonUp(0)){ //your way wouldn't work right, since you just increased nextFire
 if(!falsePull)
 {
 nextFire = Time.time + pullTime; // this is the actual fire rate as things stand now
  animation.Play("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
 
 }
 else
 falsePull = false;
 }

I think this should give you the idea, (untested)..

Comment
Add comment · Show 1 · 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
avatar image ryno9788 · Sep 18, 2012 at 12:43 AM 0
Share

Thanks it worked pretty well too thanks

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

a problem with shooting scripts 3 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Enemies Shoot Towards Face, Would Like to have them Shoot to Chest 1 Answer

Only one cloned enemy shoots 2 Answers

Cycling through enum with key press 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