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 JonnyM · Aug 17, 2015 at 10:32 PM · unity 5physicsvector3

Pushing a ball in a direction

Gawd, I really hope someone on here can help me out here. I'm testing basics of physics - similar to a golf putting game.

The Scenario I've got a default Unity scene, with a sphere and a terrain. The camera is in default position. The ball has the size and mass of a golf ball and all physics settings and friction etc are default. The ball has a rigid body and sphere collider.

I also have this bit of code attached to the ball...

 Rigidbody rigidBody;
 bool StartedShot;
 Vector3 shotStart;
 Vector3 shotEnd;
 Vector3 direction;
 float distance;
 float forceAdjust = 0.05f;
 
 
 void Start () 
 {
     rigidBody = this.GetComponent<Rigidbody> ();
     StartedShot = false;
 }
 
 
 void Update () 
 {
     if (Input.GetMouseButtonUp(1))
     {
         rigidBody.velocity = Vector3.zero;
         this.transform.position = Vector3.zero;
         StartedShot = false;
     }
 
     // Starting shot
     if (!StartedShot && Input.GetMouseButtonDown (0)) 
     {
         StartedShot = true;
         shotStart = Input.mousePosition;
     }
 
     // Ending shot
     if (StartedShot && Input.GetMouseButtonUp (0)) 
     {
         shotEnd = Input.mousePosition;
         direction = shotEnd - shotStart;
         float distance = direction.magnitude;
         StartedShot = false;
 
         Vector3 shootDirection = new Vector3(direction.x, 0.0f, direction.y);
 
         rigidBody.AddForce(shootDirection * rigidBody.mass * forceAdjust, ForceMode.Impulse);
     }
 }


Now, when I run this - I can click and drag the mouse to apply a force on the ball. And although the ball does appear to go in the right direction, when you fire it in any other direction than left - it jumps (sometimes really high). If you fire it left, it 'rolls' more like I'd expect on a flat surface.

I'm guessing this has something to do with the lack of translation from the camera position to pushing the golf ball. I'm a bit confused by how the world space coordinates relate to the screen coordinates.

So how can I fire this ball based on the drag direction on screen, without the ball jumping to all directions other than left. How do I correctly translate the mouse input vectors to firing the ball in the right direction?

Thank you kindly in advance, JM

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 Winterblood · Aug 17, 2015 at 11:30 PM

What you've done looks fine, assuming your camera is always the same orientation. I suspect your ball might be slightly embedded in the surface, so when your force is applied it tries to unembed and pings upwards. Try raising the ball so it drops slightly onto the floor.

If you do want to convert properly from screenspace to worldspace, try this:

 Matrix4x4 m = Camera.main.cameraToWorldMatrix;
 m.SetRow( 3, Vector3.zero );  // Clear pos, leave orientation only
 direction.y *= -1.0f;  // Invert Y...screen Y goes down, world Y goes up
 Vector3 shootDirection = m.MultiplyPoint( direction );

That will give you an accurate vector in true 3D. But you probably still want your force to be in the horizontal plane, which would mean using a customised matrix:

 Matrix4x4 m = Camera.main.cameraToWorldMatrix;
 m.SetRow( 3, Vector3.zero );  // Clear pos, leave orientation only
 m.SetRow( 2, Vector3.down );  // Force local Z to look down (as if cam is overhead)
 m.SetRow( 1, Vector3.Cross( m.GetRow(0), m.GetRow(2)).normalized ); // Fix local Y
 direction.y *= -1.0f;  // Invert Y...screen Y goes down, world Y goes up
 Vector3 shootDirection = m.MultiplyPoint( direction );

Code not tested but should get you most of the way there ;)

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 JonnyM · Aug 18, 2015 at 08:22 AM 0
Share

Hi, thanks for the reply.

I'd also considered that the ball might be slightly embedded but it starts a little above the terrain and drops down when the demo begins. The odd thing is, if you move the ball anywhere on the surface - it only ever rolls when going left. I've also tried moving all the scene objects to various locations, such as positive coordinates - but same thing happens.

I'll give the above a go and see if I can get that working because ultimately I will need to move the camera. Just wanted to get some basics working first. :)

avatar image JonnyM · Aug 18, 2015 at 08:25 AM 0
Share

Just tried your suggestion to map camera coordinates and although it does work when I move the camera around, I still have the problem with the ball jumping (to any direction other than left). I also notice that the drag direction doesn't seem accurate. For instance, it's very easy to drag to the left - but try dragging straight down or in a diagonal. The movement code now looks like this (I couldn't get it working unless I changed $$anonymous$$ultiplyPoint to $$anonymous$$ultiplyPoint3x4...

 shotEnd = Input.mousePosition;
 direction = shotEnd - shotStart;
 float distance = direction.magnitude;
 StartedShot = false;
 
 $$anonymous$$atrix4x4 mat = Camera.main.cameraToWorld$$anonymous$$atrix;
 mat.SetRow(3, Vector3.zero);
 mat.SetRow(2, Vector3.down);
 mat.SetRow(1, Vector3.Cross(mat.GetRow(0), mat.GetRow(2)));
 direction.y *= -1.0f;
 Vector3 shootDirection = mat.$$anonymous$$ultiplyPoint3x4(direction);
 
 rigidBody.AddForce(shootDirection * rigidBody.mass * forceAdjust, Force$$anonymous$$ode.Impulse);
 

I've uploaded the test project in case anyone can shed some light on this. (I couldn't attach to this post because I'm a noob poster, so it's hosted on my own site).

Test project is here

Appreciate the help so far. Almost gave up on this!

avatar image JonnyM · Aug 18, 2015 at 04:54 PM 0
Share

Just a side note, it seems the terrain is causing the jump issue. If I move ONLY the terrain around and retry the demo, the ball jumps in different directions. :-/

avatar image JonnyM · Aug 18, 2015 at 05:22 PM 0
Share

For anyone who's interested, this seems to be an issue when using small terrains. I defined $$anonymous$$e as 4 x 8 (really small) and the ball pings in all sorts of directions. However if I increase that to 20 x 20, the ball rolls smooth in all directions with no additional changes.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2.5d Player Controller 0 Answers

Why does the train jump? 1 Answer

Rigidody.velocity = Direction * speed; How to get direction? 1 Answer

Ball with bounciness 1 bounces higher and higher in unity2d,Ball jumps higher and higher when bounciness set to 1 1 Answer

Softbodies in Unity 3D 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