- Home /
Question by
ImAnonymouss · Jul 31, 2018 at 04:19 PM ·
c#axisforcenewbie
Stop certain Axis Force in a certain condition.
I want to make my GameObject move to the right while i hold down a button and until there ive done a decent job. However when i dont hold down the button anymore i want the z force to be set to 0. I've been trying a few things however i cant get it to work. Btw i've just started out in unity and im still getting the hang off C#, however i do know the basics but still, sorry if im asking something dumb. thx in advance.
This my code and u can see that there is a huge flaw in it.
public bool ispressedRight = false;
public Rigidbody rb;
public int rightSpeed = 150;
void Update () {
if (ispressedRight == true)
{
rb.AddForce(0, 0, rightSpeed * Time.deltaTime);
}
else
do
{
rb.AddForce(0, 0, -rightSpeed * Time.deltaTime);
}
while (rb.velocity.magnitude != 0);
}
public void OnPointerDown(PointerEventData eventData)
{
ispressedRight = true;
}
public void OnPointerUp(PointerEventData eventData)
{
ispressedRight = false;
}
Comment
Your answer
Follow this Question
Related Questions
How To Accelerate/Decelerate Gradually With Torque GetAxis 1 Answer
Multiple Cars not working 1 Answer
AddExplosionForce help! 1 Answer
Distribute terrain in zones 3 Answers
Force input on Button? 1 Answer