- Home /
Question by
ElucardL · Apr 23, 2019 at 09:48 AM ·
movementrigidbodytouchmovement scriptrigidbody.addforce
How to make a rigidbody move in the direction of finger(x,y,z)
For example, If I swipe the finger left, I want my rigidbody move in left direction. Please, help.
Comment
Answer by rohitkvasanth · Apr 23, 2019 at 10:19 AM
Something like this. i wrote it here directly, do check before writing in IDE. Instead of v3.left and v3.right, use the direction you want to move.
public void OnEndDrag(PointerEventData eventData)
{
dragDirection = (eventData.position - eventData.pressPosition).normalized;
bool isRight= dragDirection.x>0 ? true:false;
switch (isRight)
{
case true : player.transform.GetComponet<RigidBody>().velocity = Vector3.right; break;
case false: player.transform.GetComponet<RigidBody>().velocity = Vector3.left; break;
}
}
GetComponent <RigidBody>().velocity
I told you, I wrote directly here. sorry for the typo.