- Home /
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
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 ;)
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. :)
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).
Appreciate the help so far. Almost gave up on this!
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. :-/
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.