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 Dave M · Dec 14, 2010 at 11:52 PM · mouseaddforceforceshoot

if mouse button is pressed continual add force from min. to max.and then shoot a ball

Hallo is there anybody who will help me with this code???

var cannonball : Transform; var power = 0; var speed = 5; var powermax = 5000; var powermin = 100;

var timeToMove : float = 0.5; // time to move (in seconds) private var lerpAmt : float = 0.0; // current lerp 't' amount (private, 0-1)

if ( Input.GetButtonUp("Fire1") // if pin is pressed && lerpAmt < 1 ) // and lerpAmt is not already at max

{
lerpAmt += Time.deltaTime / timeToMove; power = Mathf.Lerp(powermin, powermax, lerpAmt); }

function Update() { if(Input.GetButton("Fire1")) { var projectile = Instantiate(cannonball, transform.position, transform.rotation); projectile.rigidbody.AddForce(transform.right * power);//cannon's x axis Physics.IgnoreCollision(projectile.collider, collider); }

 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Statement · Dec 15, 2010 at 03:20 AM

Try this script. I tested it and it seems to work just fine. If you don't want to automatically shoot when powermax is reached, uncheck Auto Throw in inspector. Easy.

var cannonball : Transform; var speed : float = 500; // speed is power per second var powermax : float = 5000; var powermin : float = 100; var autoThrow : boolean = true;

function Update() {
if (Input.GetButtonDown("Fire1")) StartCoroutine("Cook"); }

// This is a co-routine. // I used the term "to cook a grenade" as popular in many action games. function Cook() { var power : float = powermin;

 while (Input.GetButton("Fire1"))
 {
     var move = speed * Time.deltaTime;
     power = Mathf.MoveTowards(power, powermax, move);

     if (autoThrow &amp;&amp; power == powermax) 
         break;
     else 
         yield;
 }

 Shoot(power);

}

function Shoot(power : float) { var projectile = Instantiate(cannonball, transform.position, transform.rotation); projectile.rigidbody.AddForce(transform.right * power); Physics.IgnoreCollision(projectile.collider, collider); }

Comment
Add comment · Show 4 · 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 Dave M · Dec 15, 2010 at 03:54 AM 0
Share

It works great!!!$$anonymous$$any thanks

avatar image madmike6537 · Nov 16, 2012 at 11:31 PM 0
Share

Darn,

I am trying to use this but I need it to fire when you release the button, I dont believe this functions that way.

avatar image Statement · Dec 03, 2012 at 11:17 PM 0
Share

@madmike6537 It should fire when you release the button. Just make sure that autoThrow is set to false. (Press, hold & release)

avatar image madmike6537 · Dec 03, 2012 at 11:58 PM 0
Share

Thanks! I ended up getting this to work perfectly, forgot to update my comment! Thanks again :)

avatar image
0
Best Answer

Answer by Justin Warner · Dec 15, 2010 at 12:30 AM

if ( Input.GetButtonUp("Fire1") // if pin is pressed && lerpAmt < 1 ) // and lerpAmt is not already at max

{
lerpAmt += Time.deltaTime / timeToMove; power = Mathf.Lerp(powermin, powermax, lerpAmt); }

That isn't in a function, it needs to be so.

------------EDIT------------------

var cannonball : Transform; var power = 0; var speed = 5; var powermax = 5000; var powermin = 100; var timeToMove : float = 0.5; // time to move (in seconds) private var lerpAmt : float = 0.0; // current lerp 't' amount (private, 0-1)

function Update() {

 if ( Input.GetButtonUp ( "Fire1" ) &amp;&amp; lerpAmt &lt; 1 )                // and lerpAmt is not already at max
 {
     lerpAmt += Time.deltaTime / timeToMove;
     power = Mathf.Lerp ( powermin, powermax, lerpAmt );
 }

 if ( Input.GetButton ( "Fire1" ) )
 {
     var projectile = Instantiate ( cannonball, transform.position, transform.rotation );
     projectile.rigidbody.AddForce ( transform.right * power );//cannon's x axis
     Physics.IgnoreCollision ( projectile.collider, collider );
 }

}

That's it cleaned up and in the Update function...

Comment
Add comment · Show 6 · 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 Dave M · Dec 15, 2010 at 01:39 AM 0
Share

Thanks for your help but Im not able to fix the problem....could someone write this code clean

avatar image Justin Warner · Dec 15, 2010 at 02:14 AM 0
Share

Edited answer to be more neat and in the Update function...

avatar image Dave M · Dec 15, 2010 at 03:19 AM 0
Share

$$anonymous$$any thanks for your fast answer.Is there a fast way to make it reverse if i leafe the mouse button??

avatar image Statement · Dec 15, 2010 at 03:22 AM 0
Share

That doesn't make any sense Dave. You wanted to have continual force between $$anonymous$$ and max. How are you ever going to shoot it "half way through"? Sit down and think through what it is you really want.

avatar image Dave M · Dec 15, 2010 at 03:46 AM 0
Share

Ok sorry, I mean If I hold the mouse button continual and the force goes to max. in 5 sec.( and I still hold the button but the force doesent rise) and if I leaf the mouse button the force goes down to 0. And the ball will be pushed. Hope you understad that....

Show more comments

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

[Solved] How to shoot an object in front of the player? 1 Answer

Applying force to an object without rotating it. 2 Answers

Shoot at Mouse Cursor 1 Answer

Rigidbody force problem 1 Answer

Add force on camera using AddForce 2 Answers


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