How do I make a rigid body move towards position of touch
I want a sphere to only move either left or right (using rigidbody.addforce) depending on where the screen has been touched. I used the following code but the sphere moves very slowly.
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
Vector3 touchPosition = Input.GetTouch(0).position;
double halfScreen = Screen.width / 2.0;
if(touchPosition.x < halfScreen)
{
rigidbody.AddForce(Vector3.left * speed * Time.deltaTime);
}
else if (touchPosition.x > halfScreen)
{
rigidbody.AddForce(Vector3.right * speed * Time.deltaTime);
}
}
This code was the solution from another thread where the OP was having the same problem. But it's not working for me. I'm trying to develop for the android platform. Are there any alternatives? Is there a way I can re-write the code using mouse click functions (so that android converts it to touch after the game is built)?
What's speed set to? Or more to the point set it to higher.
Really though you'll likely want it to stop when it gets there so maybe use move towards ins$$anonymous$$d.
At first, the speed was set to 7. At that speed, the sphere only tilted a bit (I could tell because of the design I put on the sphere). So I set the speed to 20 just to see what happens. At that speed, the sphere moves REALLY slowly. I should let you know that I've been testing the game using keyboard controls for the past couple of weeks and the movement of the sphere was perfect. But now I'm thinking of switching to touchscreen inputs because I want to release it for Android. This is the code I've been using on the sphere for keyboard inputs:
float horizontal = Input.GetAxis ("Horizontal");
Vector3 movement = new Vector3 (horizontal, 0.0f, 0.0f);
rigidbody.AddForce (movement * speed);
The sphere moves fine with the Left and Right keyboard buttons when I use this code. But now, I want to use touch screen inputs ins$$anonymous$$d.
@TheSaviour - After checking your touch code vs. your keyboard code, I noticed the touch code is adding force using Time.deltaTime in the calculation, while your keyboard code is not using it. This could account for your speed difference, ya? :)
Yep Time.DeltaTime is (or should be) in fractions as it's 1 second divided by the current frames per second and multiply by a fraction is like divide by a whole number. It's not a bad thing to do though as it means the force will move the sphere X distance in X seconds on all platforms regardless of frames per second.
You can just up the force you apply but I really think you'd be better using move towards as adding the exact amount of force to move sphere exactly to a point is going to be a lot harder.
How can I use $$anonymous$$oveTowards in this script? I can't find any info on implementing Vector3.$$anonymous$$oveTowards on a rigid body as a reaction to touch input...
Do you specifically need the rigidbody or are you just using that so you can add force?
If the former then try something like this...
rigidbody.AddForce((target.position - transform.position) * 2000 * Time.deltaTime);
Or try using rigidbody.$$anonymous$$ovePosition.