How do I add ForceMode.VelocityChange to this code, I made a code that moves the player right or left depending on the side of screen u touch on the phone, but when I touch the player continues going and not stoping so here is the code I want ForceMode.VelocityChange to add to @Brackeys
public class Touchcon : MonoBehaviour
{
public float touchspeed= 300f;
public GameObject player;
private Rigidbody Rb;
private float ScreenWidth;
// Start is called before the first frame update
void Start()
{
ScreenWidth = Screen.width;
Rb = player.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
int i = 0;
while (i < Input.touchCount)
{
if (Input.GetTouch(i).position.x > ScreenWidth / 2)
{
RunPlayer(1.0f);
}
if (Input.GetTouch(i).position.x < ScreenWidth / 2)
{
RunPlayer(-1.0f);
}
++i;
}
}
private void FixedUpdate()
{
#if UNITY_EDITOR
RunPlayer(Input.GetAxis("Horizontal"));
#endif
}
private void RunPlayer(float honrizontalInput)
{
Rb.AddForce(new Vector3(honrizontalInput * touchspeed * Time.deltaTime, 0 ));
}