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 united4life · Feb 13, 2013 at 01:05 PM · projectilewind

How to implement wind for a projectile motion?

I have already done a projectile using mouse buttons and all. Now everytime i kick a ball there should be a random wind amount so that i kick the ball and the force changes according to the wind. For eg take the pocket tanks game evertime it generates a random wind value. That random wind i can generate through random function but i am not getting how to change the force direction and all.

Here is the script i have used

 Rigidbody initialBall = null;
 public Rigidbody ball;
 
 Vector2 firstPressPos;

 Vector2 secondPressPos;

 Vector2 currentSwipe;
 
 float xAngle, yAngle;
 
 void Start () 
 {
     SpawnBall();
 }
 
 public void SpawnBall()
 {
     initialBall = (Rigidbody)Instantiate(ball, transform.position + new Vector3(0, 0.3f, 3f), Quaternion.identity);
 }
 
 void Update () 
 {
     if(initialBall)
     {    
         if(Input.GetMouseButtonDown(0)) 
          {
                firstPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
          }
 
         if(Input.GetMouseButton(0))
         {
             secondPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);

             currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y); 

                currentSwipe.Normalize();
         
             if(currentSwipe.y > 0 )
             {
                 yAngle += currentSwipe.y * 40f;

             }
             
             if(currentSwipe.x < 0 )
             {
                 xAngle += currentSwipe.x * 20f;
             }


             if(currentSwipe.x > 0 )
             {
                 xAngle += currentSwipe.x * 20f;

             }
         }
         
         if(Input.GetMouseButtonUp(0))
         {
             if(yAngle> 300)
                 initialBall.AddForce(xAngle, yAngle, yAngle);
                 
             xAngle = 0f;
             yAngle = 0f;
         }
     }
 }
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

Answer by Dave-Carlile · Feb 13, 2013 at 01:39 PM

Wind is just another force, like gravity. It's represented by a vector that points in the wind direction.

A good method to give you a random wind direction and force is Random.insideUnitySphere.

 float MaxWindForce = 5;
 Vector3 windForce = Random.insideUnitSphere * MaxWindForce;

Then do an AddForce on your ball each frame (scaling the force by Time.deltaTime), or use ConstantForce.

 initialBall.AddForce(windForce * Time.deltaTime);

Since the force is a vector, you can get the strength of the wind by using the vector magnitude.

 float strength = windForce.magnitude;


Comment
Add comment · Show 9 · 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 united4life · Feb 14, 2013 at 09:29 AM 0
Share

This will make the ball move according to the wind but i want the trajectory to change according to the wind.

Suppose the ball reaches the max height of the trajectory then at that particular time the trajectory should change according to the wind.

avatar image united4life · Feb 14, 2013 at 09:32 AM 0
Share

like in this game

http://www.youtube.com/watch?v=Jh213sc9Of8

the trajectory changes after some time

avatar image MojopinStudios · Feb 14, 2013 at 12:30 PM 0
Share

That is not what's happening in that game. Dave Carlisle's answer is what you need. Did you even try it?

avatar image united4life · Feb 14, 2013 at 12:35 PM 0
Share

yeah i tried it was helpful but thats not what i wanted. The thing is ball is moving with constant force but at some point i want to change the force

avatar image MojopinStudios · Feb 14, 2013 at 12:41 PM 0
Share

But it is an extra force, not the only force. When you want the wind to affect the ball, add the extra force to the ball. So you will have the initial force that propelled it forward and the force of the wind acting upon it.

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

11 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

Related Questions

Could you advice me why this Gun and Ballistic Script doesn't work? 1 Answer

Basic projectile trajectory with transform 1 Answer

Gun Fire, sparks on Collision 1 Answer

how to differentiate clones? 1 Answer

Projectiles without colliders or rigid bodies? 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