The question is answered, right answer was accepted
How can i run a script with a single key press?
Hello, I'm messing around in Unity and I want force to apply to a cube only one when i press "s"
I have tried this following method:
public float force = 5000f;
if (Input.GetKeyDown (KeyCode.S)) rb.AddForce(0, 0, force); Debug.Log("Launch");
if (force > 5000) force = 5000;
if i repeatedly "s" then the force will increase by increments of 5000 rather than staying at 5000 now matter how many times i press the s key.
using UnityEngine;
public class player : MonoBehaviour
{
public Rigidbody rb;
public float force = 5000;
public float sideforce = 500;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
rb.AddForce(0, 0, force);
Debug.Log("Launch");
if (Input.GetKey(KeyCode.RightArrow))
{
Debug.Log("Left Movement");
rb.AddForce(sideforce * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
Debug.Log("Right Movement");
rb.AddForce(-sideforce * Time.deltaTime, 0, 0);
}
}
}
Thank you
if you want to the collider stop when you release the arrow, consider using velocity or increase Drag on rigidbody options.
Answer by corzokidmcv · Feb 10, 2019 at 05:07 PM
found the solution, had to change it to if (Input.GetKeyDown(KeyCode.S)) { rb.AddForce(force * transform.forward); }
Follow this Question
Related Questions
Rotate Player 90 degrees about its Y axis relative to the mouse being dragged between two angles 1 Answer
Can't get my ball to move. 0 Answers
How to disable raycast/groundchecker on OnTriggerEnter? 0 Answers
my script was written but does not want to work 1 Answer
Issues using two scripts. 1 Answer