- Home /
how do I stop adding force after the button is released,How do i stop adding force after I release the button in Unity3d?
Im a beginner! Please help me im trying to make my first game!!! The problem is that the force keeps on going even when the button is released
i want to stop adding force after i let go of the button and keep the force on when i hold the button
IT IS A 3D GAME
MY CODE:--
....................................................................................
public Rigidbody rb;
public float ForwardForce = 2000f;
public float SideForce = 200f;
void FixedUpdate()
{
rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
}
//from here left right movment code
public void MoveLeft()
{
rb.AddForce(-SideForce * Time.deltaTime, 0, 0);
}
public void MoveRight()
{
rb.AddForce(SideForce * Time.deltaTime, 0, 0);
}
} ...................................................................................,Im a beginner! Please help me im trying to make my first game!!! The problem is that the force keeps on going even when the button is released
i want to stop adding force after i let go of the button and keep the force on when i hold the button
IT IS A 3D GAME
MY CODE:--
.................................................................................... using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float ForwardForce = 2000f;
public float SideForce = 200f;
void FixedUpdate()
{
rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
}
//from here left right movment code
public void MoveLeft()
{
rb.AddForce(-SideForce * Time.deltaTime, 0, 0);
}
public void MoveRight()
{
rb.AddForce(SideForce * Time.deltaTime, 0, 0);
}
} ...................................................................................
Answer by Zubzuke · Nov 15, 2020 at 01:44 PM
The simplest way would be to add a drag force to your rigidbody. You can adjust the Drag value in the inspector.
Answer by orteogames · Nov 15, 2020 at 04:51 PM
@Infinix_ i was just workin on this thing today......
i tried so many ways.. but settled finally with one*(the only one which worked)*
public Rigidbody rb;
public float ForwardForce = 2000f;
public float SideForce = 200f;
bool leftbtpressed, rightbtnpressed;
void Start()
{
rb = GetComponent<Rigidbody>();
leftbtpressed = false;
rightbtnpressed = false;
}
public void Leftbtndown()//add an eventtrigger to your left button and add "on pointer down() event"
{
leftbtpressed = true;
}
public void Leftbtnup()/add an eventtrigger to your left button and add "on pointer up() event"
{
leftbtpressed = false;
}
public void Rightbtndown()/add an eventtrigger to your right button and add "on pointer down() event"
{
rightbtnpressed = true;
}
public void Rightbtnup())/add an eventtrigger to your right button and add "on pointer up() event"
{
rightbtnpressed = false;
}
void FixedUpdate()
{
rb.AddForce(0, 0, ForwardForce * Time.deltaTime);
ToMoveorno();
}
public void ToMoveorno()
{
if (leftbtpressed)
{
rb.AddForce(-speed * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
else if (rightbtnpressed)
{
rb.AddForce(SideForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
after ataching this script to your player..... add an event trigger component to both of your left and right btns..... and then add 2 event types to both of your btns, one is: onpinterdown..... and the other is: onponterup.... and do the required....
if you dont know how to do this skip to 3:24 in ttis vid.. but dont add what he adds in the vid to the event system instead add "onpinterdown" and "onponterup" events and do what you do for the default on click() that comes in a button.
https://www.youtube.com/watch?v=3NBYqPAA5Es
also while finding that video i found another vid which uses the exact script like mine you can watch this instead of that...
https://www.youtube.com/watch?v=Qy7f75CElCo
I hope this helps helps you :)
hey man where do i add the buttons??? do i have to make the buttons children of the player??
Hey help bro this is my instagram id: a.d.v.a.i.t.h_31
txt me if u can and i'll give u all my reward points
Hey thanks brooo!! i figured it out how to do it!! im giving u all my reward points!!
Your answer
Follow this Question
Related Questions
IsPointOverGameObject() not working as intended!!! 2 Answers
How do i Update a Transforms Position, and how do i have objects face along a spline?? 1 Answer
Ui image appear and disappear on trigger 1 Answer
Objects are not visible until the camera gets closer. 1 Answer
Unity Standard Assets First Person Controller Camera Not Working Correctly 0 Answers