• 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 1 · Mar 12, 2010 at 09:12 PM · physicsiphoneballoon

help me blow my balloon....

I am trying to create a prototype of a balloon that can me affected on a 2D plane by the users mouse clicks. Depending on the location i want to fire an AddForce function to push the balloon AWAY from the users click. here's what i'm trying to work with...

if (Input.GetButtonDown ("Fire1")) { // Construct a ray from the current mouse coordinates var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

        var objVect: Vector3;

        var mouseVect: Vector3;

        mouseVect[0] = (Input.mousePosition.x);
        mouseVect[1] = (Input.mousePosition.y);
        mouseVect[2] = (0);

        print ("mouseVect is " + mouseVect);

        objVect[0] = (myRigidBody.position.x);
        objVect[1] = (myRigidBody.position.y);
        objVect[2] = (0);

        if (Physics.Raycast (mouseVect, objVect))
           var forceX;
           var forceY;

           var multiplyer = .05;

           forceX = ((Input.mousePosition.x) - (myRigidBody.position.x)) * multiplyer;
           forceY = ((Input.mousePosition.y) - (myRigidBody.position.y)) * multiplyer;

           //print ("forceX is " + forceX);
           //print ("forceY is " + forceY);

           myRigidBody.AddForce (forceX, 0, 0);
           myRigidBody.AddForce (0, forceY, 0);
        }
  } 

I think the problem is that i want to use the AddForce(Physics.Ratcast(mouseVect, objVect)) as my force but don't know how that's done.

Any help would be AWESOME!!!

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
2

Answer by Flimgoblin · Mar 12, 2010 at 09:18 PM

Just replied on your forum post but as this is meant to be the place I'll reply here too:

OK, looking at the docs this function that andeeee mentioned before is what you need.

http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenToWorldPoint.html

This effectively does the raycasting/collision with the plane for you (which is the long way around I was suggesting before).

However, you need to set a Z position for it (which was missing before) - this is the distance forward from the camera. In your case you want that to be the Z position of the balloon relative to the camera.

So:

public var pushForceMultiplier:float=100.0; // you'll want to tweak that

function Update(){

 if(Input.GetMouseButtonDown(0)){
 // left click

   var camDist=rigidbody.position.z-Camera.main.transform.position.z;

   var clickPoint=Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y,camDist)); // screen click point on the same Z distance from the camera as your rigidbody


   var vec=rigidbody.position-clickPoint; // the direction from clickPoint to the rigidbody
   vec=vec.normalized;

   rigidbody.AddForce(vec*pushForceMultiplier);

 }


}

NB. This is written as a script you attach to the balloon, rather than something you set a variable on linking to it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Physics collider behaving very odd on iphone 0 Answers

How can I keep small dynamic physics objects from occasionally falling through a mesh collider? 1 Answer

Infinite swing with SpringJoint 2 Answers

Strange transform hierarchy problem when adding a rigidbody into the hierarchy 2 Answers

I am having a problem with momentum of an object after releasing a touch from the iPhone. 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