- Home /
Rigidbody swipe
Hi guys,
I'm on a new project not stricly gaming but physics oriented. Basically we are talking about a ball that spins left or right accordingly to the touch swipe direction and speed. The faster you swipe the faster the ball will spin. I have to use rigidbody and addTorque for this stuff and it's working pretty well.
Each swipe adds speed to the ball when it's spinning and the result is pretty realistic. (I'm using EasyTouch 3.0)
void On_Swipe(Gesture gesture)
{
ballSpeed = MathF.Abs(gesture.swipeDelta);
If(gesture.swipeDelta > 0)
dir =1;
else
dir =-1;
ball.rigidbody.addTorque(vectorUp * ballSpeed *dir);
}
void Update()
{
// we cut over time the speed so the ball will stop at some point.
If(Ballspeed > 0 && dir = 1)
ballSpeed -= friction;
else if (Ballspeed > 0 && dir = 1)
ballSpeed += friction;
}
The problem comes when you have the finger stationary on the ball and want to move the ball a little bit left or right. The effect I want to recreate here is that you are actually touching the balla and you can move it left or right but the touch point stays under your finger.
So I have this code in place:
void On_Drag(Gesture gesture)
{
if(gesture.pickedObject.tag == "ball" && gesture.actionTime > 0.2f)
//FingerTouch Execute each frame this function.
ball.transform.rotation = new Vector3(0,gesture.swipedelta*Time.deltaTime,0);
//Here I must store the ball rotation speed. Will explain later why. I'm not sure this is the rot speed anyway
ballSpeed = ball.rigidbody.velocity.y;
}
Now if we have the finger on the ball swipe fast and lift the finger the ball must spin like when you "launch" the Wheel of the fortune (:D). So I tried to store the ball speed and use it on the rigidbody
void On_FingerUp(Gesture gesture)
{
// we must be sure that we store the last speed
If(gesture.swipeDelta > 0)
dir =1;
else
dir =-1;
ballSpeed = gesture.swipedelta.x / gesture.actionTime;
ball.rigidbody.addTorque(vectorUp * ballSpeed *dir);
}
The major problem here are that when I rotate the ball the rotation are not precise. The point you touch is not always "under you finger" when you move the ball. Moreover the "launch" feature is really bad and snappy. Any guess why? At the start seemed so simple: just replicate the feeling of touching and spinning a ball :(
Your answer
Follow this Question
Related Questions
AddTorque not increasing relativeVelocity magnitude of a collision 1 Answer
Spinning a sphere in place causes unwanted velocity. 1 Answer
Rigidbody rotation around CenterOfMass 1 Answer
how to set initial speed of vehicle 1 Answer
Calculate force for torque by specifying a required velocity 1 Answer