Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AizenSousuke · Oct 04, 2011 at 08:18 AM · physicsaddforceforce

Applying force to an object without rotating it.

I'm trying to replicate smth like Angry Bird's catapult system whereby (in my case), the player presses on the object then drag his fingers backwards, without the object being dragged, and when he releases his finger, the object will fly/go towards the direction opposite of where he lift off his finger.

Here is my code:

 var bSelected : boolean = false;
 var bIsMoving : boolean = false;
 
 private var forceToUse : Vector3;
 private var direction : int;
 
 function Update () {
     //Prepare for launch!
     Prepare();
 }
 
 //If clicked
 function OnMouseDown () {
     //When the thing is not moving and isn't already selected
     if (bIsMoving == false && bSelected == false) {
         bSelected = true;
     }
 }
 
 //If not clicked
 function OnMouseUp () {
     //When the thing stops moving and is already selected beforehand
     if (bIsMoving == false && bSelected == true) {
         //Shoot code here
         //rigidbody.AddRelativeForce(forceToUse);
         
         //Reset back
         bSelected = false;
         //Debug.Log(bIsMoving);
         //Debug.Log(bSelected);
     }
 }
 
 function Prepare() {
     if (bSelected && !bIsMoving) {
         //Where this object is at
         var imHere = transform.position;
         //We don't want the z axis (depth)
         imHere.z = 0;
         //Debug.Log(imHere);
         //Find where is the mouse at
         var mouseIsAt = Input.mousePosition;
         mouseIsAt.z = 0;
         //Debug.Log(Input.mousePosition);
         
         //Force to use - Difference between the two
         forceToUse = mouseIsAt - imHere;
         
         //Direction
         direction = Vector3.Angle(mouseIsAt,imHere);
         Debug.Log(direction);
         
     }
 }
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
0

Answer by asafsitner · Oct 04, 2011 at 08:59 AM

Look for 'constraints' section in the Rigidbody component. You can also save yourself the need to zero the Z axis value.

Comment
Add comment · Show 3 · 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 AizenSousuke · Oct 04, 2011 at 02:22 PM 0
Share

Huh? That only limits me to the movement. But how do I make the movement in the first place? (like the above example in my qn).

avatar image asafsitner · Oct 04, 2011 at 09:27 PM 0
Share

That limits either rotation or movement (or both) on either of the axes. To actually apply the force use Rigidbody.AddForce(direction * forceToUse); as seen in the documentation:

http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html

avatar image AizenSousuke · Oct 05, 2011 at 03:13 AM 0
Share

ok.

but how do I get the direction? Let's say my origin is 0,0 and my thumb is at -1,-1. So what's the direction of my catapult in this case? (How to calculate it to be precise)

avatar image
0

Answer by burrowgames · Oct 05, 2011 at 04:45 AM

You want to get a Normalize the vector in the direction you want to go. If your thumb is at <-1,-1> you want the object to go to <1,1>. If your thumb is at <-5,-2>, you want your object to head in the direction of <5,2>. However you need to normalize it.

So what you do is calculate the length of the vector. Length^2 = x^2 + y^2.

Then you divide the vector you calculated, say <5,2> by the length, which in that case will be 5.385, so you end up with a vector <0.92847, 0.3714>

Now what you have is a Unit vector in the direction you want to go. You can apply your own force to that by simply multiplying your force. Although in your case you might want your force based on the distance the user is from the projectile... You can normalize, and based on the distance clamp it to a maximum value, say you want your force to only be 10 max per direction. So you would check to see if your length vector is greater than a specified amount, if it is, then normalize it, and multiply that value by your maximum force.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Force applied to a Sphere from a paddle(Cube,Rectangle) 1 Answer

Realistic methods of propulsion without friction involved 0 Answers

Add Force without Rotation 2 Answers

FixedUpdate vs. Update for instantaneous ForceMode's 1 Answer

add force to object that has 2 different rigid bodies 0 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