Basically, I have a ball that rolls/moves with addForce.
The goal:
Touch and drag on screen to move the ball using addForce not transform.translate and have it move accordingly.
The problem:
My code for it works perfectly with a little problem. The rate at which you drag across the screen makes the ball move that much faster, this is bad for me because if you swipe fast enough the ball will go flying far far away. I need the whole screen to act more like a joystick, as in hold and drag in a direction it can only go so fast and never just super sling shot away at the speed of your swipe.
The Code:
if (Input.touches.Length > 0 && Input.touches[0].phase == TouchPhase.Moved)
{
float x = Input.touches[0].deltaPosition.x * speed * Time.deltaTime;
float y = Input.touches[0].deltaPosition.y * speed * Time.deltaTime;
rigidbody.AddForce (new Vector3(x, 0, y));
}
It moves properly if you drag slowly. But the moment you drag quickly, it shoots off in that direction with speeds you wouldn’t believe. I have tinkered and tinkered but cannot get a cap on the speed or drag speed. Thanks for your time.
EDIT-------------------
I changed it to this:
float x = Input.touches[0].deltaPosition.x;
float y = Input.touches[0].deltaPosition.y;
Vector3 movement = new Vector3 (x, 0.0f, y);
rigidbody.AddForce (movement * speed * Time.deltaTime);
Got a little less crazy response, still can shoot it out but not as easily… Don’t exactly understand why it helps…
At least with this I separated the deltaPosition and multiplication of speed and time.deltaTime.
Maybe now I can put some sort of cap to the length of touches? Idk, I dont exactly understand this code.