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 youngo · Mar 12, 2012 at 11:13 AM · physicsmousespeed

How to realize the real world physics?

For example, if i drag an object with mouse,when i throw it to the sky,the object will have a x-axis speed and a gravity,so the object will drop off very elegant and nice,but i can't work it out.It make me very anxiety.Thanks anyway~

Comment
Add comment · Show 1
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 Kleptomaniac · Mar 12, 2012 at 11:17 AM 0
Share

It 'make you very anxiety'? Perhaps you should consider not using Unity if it make you so anxiety. :D

1 Reply

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

Answer by save · Mar 12, 2012 at 12:24 PM

Do not worry, sit back, have a valium (if needed) and attach this script to the thrown object:

#pragma strict

/ Mouse Throw Physics by save Attach this script to a GameObject you would like to throw Last modified 2012-03-12 /

@script RequireComponent(Rigidbody) @script RequireComponent(Collider)

var strength : int = 20; //The strength to multiply in AddForce var AlterUseOfGravity : boolean = true; //If useGravity is off and AlterUseOfGravity is off the object will continue to float

static var cam : Camera; static var cameraTransform : Transform;

private var myTransform : Transform; private var myRigidbody : Rigidbody;

private var myMagnitude : Vector3 = Vector3.zero; private var initialPosition : Vector3; private var setZdepth : float = .0;

private var activated : boolean = false;

function Awake () { if (cam==null || cameraTransform==null) { cam = Camera.main; cameraTransform = cam.transform; } }

function Start () { myTransform = transform; myRigidbody = rigidbody; initialPosition = myTransform.position; }

function Update () { if (activated) { SetPosition(); } }

function SetPosition () { if (!myRigidbody.isKinematic) { myRigidbody.isKinematic = true; if (AlterUseOfGravity) myRigidbody.useGravity = false; setZdepth = myTransform.position.z-cameraTransform.position.z; }

 var pos : Vector3;
 pos = Input.mousePosition;
 pos.x = Mathf.Clamp(pos.x, 0, Screen.width);
 pos.y = Mathf.Clamp(pos.y, 0, Screen.height);
 var newPos : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(pos.x, pos.y, setZdepth));
 myTransform.position = newPos;  

 myMagnitude = (myTransform.position - initialPosition) /Time.deltaTime;

 initialPosition = myTransform.position;

 if (Input.GetMouseButtonUp(0)) {
     AddForce(myMagnitude);
 }

}

function OnMouseDown () { activated = true; }

function AddForce (m : Vector3) { myRigidbody.isKinematic = false; if (AlterUseOfGravity) myRigidbody.useGravity = true; myRigidbody.AddForce(m*strength); activated = false; }

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 Kleptomaniac · Mar 12, 2012 at 12:28 PM 1
Share

@save This script is awesome! Thumbs up for both awesomeness and valium! :)

avatar image save · Mar 12, 2012 at 12:29 PM 0
Share

Nice! If you can't see the updated script remove Debug.Log(my$$anonymous$$agnitude) as it was just for testing and eats CPU like crazy. :)

avatar image Bunny83 · Mar 12, 2012 at 12:36 PM 0
Share

@save: Your edit will be visible, well anytime soon ;)

I'm not sure what they changed but it's still not fixed...

avatar image save · Mar 12, 2012 at 12:40 PM 0
Share

@Bunny83 Quato has been acting up a bit lately, makes you wonder if there is a moderator that actually goes through edits or if there is a server side script that has a low refresh. I hope it is the latter as I'm an idiot when it comes to edits.. :)

avatar image Kleptomaniac · Mar 15, 2012 at 05:33 AM 1
Share

I rock! Haha, shouldn't you be saying that to @save? He was the one who gave you the script and suggested you take a valium!

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

8 People are following this question.

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

Related Questions

How to create normal physics reactions when collision is caused by a GameObject controlled by mouse position 0 Answers

Making an object fly at selected item. 0 Answers

How am I able to make sonic like contols? 1 Answer

Having more than one collider in a GameObject? 8 Answers

How does Angle of collision affect velocity? 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