if(Input.GetKeyDown(KeyCode.Space))
rb.AddForce(Vector3.up * 100f);
if (Input.GetKeyDown(KeyCode.W))
rb.AddForce(Vector3.forward * 80f);
if(Input.GetKeyDown(KeyCode.D))
rb.AddForce(Vector3.right * 80f);
if (Input.GetKeyDown(KeyCode.A))
rb.AddForce(Vector3.left * 80f)
This is the code
Try switching all your GetKeyDown calls to just GetKey.
GetKeyDown is only true on the frame the key is pressed down. GetKey is true for every frame the key is held down.
1 Like
Thank You!Also,how can I make it so that there is a cooldown for the jumping.So if you jump once you have a cooldown.
I get the impression you haven’t done any of your own searching - these are super basic questions, and I don’t wanna cut you down for asking for help - but you really need to search for quite some time to solve a vast majority of the beginner issues like this, as they have been answered in about a million different ways a million different times.
https://lmgtfy.app/?q=unity+create+a+delay
1 Like
Do you just want to use a timer? If using a timer you have the potential for double jumps if you’re still in the air when the timer runs out.
If that is what you want, you’d switch back to GetKeyDown. Then set a float variable with how much time you want to run down. Every update you subtract Time.deltaTime from that timer variable. When you check for whether to jump, you check if that timer variable is at or below 0f. There is more than one way of accomplishing the same thing, but the above will work.
Also as a side note, you should place any AddForce calls in FixedUpdate(). FixedUpdate is called immediately before the physics update. But FixedUpdate isn’t called every frame. So you don’t want to check for input with GetKeyDown in FixedUpdate, because you will sometimes miss inputs. Since GetKey is true every frame it is held down, it isn’t that bad to check in FixedUpdate. But generally the approach to player input triggering physics based movement should be you check for the input in regular old Update(), then act on the input in FixedUpdate with your physics force calls.
1 Like
Thank you. Joe-Censored. You are right! I had that problem because I had another script that used GetKeyDown Now I changed it and it is in sync with the other script!
how do i do backwards is it just backwards or something after Vector3
thanks my code is looking great now
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody myRigidbody;
public float Right;
public float Left;
public float moveSpeed;
public float MouseSensitivity;
public float deadZone = 15;
public bool isGrounded = false;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButton(0))
{
transform.eulerAngles += MouseSensitivity * new Vector3( x: -Input.GetAxis("Mouse Y"), y: Input.GetAxis("Mouse X"), z: 0);
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
GetComponent<Rigidbody>().AddForce(Vector3.up * 100, ForceMode.Impulse);
}
if (transform.position.y > deadZone)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
if (Input.GetKey(KeyCode.W))
myRigidbody.linearVelocity = Vector3.forward * 10;
if (Input.GetKey(KeyCode.Q))
myRigidbody.linearVelocity = Vector3.left * 7;
if (Input.GetKey(KeyCode.E))
myRigidbody.linearVelocity = Vector3.right * 7;
if (Input.GetKey(KeyCode.S))
myRigidbody.linearVelocity = Vector3.back * 9;
if (Input.GetKey(KeyCode.D))
transform.Rotate(transform.up ,Input.GetAxis("Horizontal") * Right );
if (Input.GetKey(KeyCode.A))
transform.Rotate(transform.up ,-Input.GetAxis("Horizontal") * Left );
}
void OnCollisionEnter(Collision collision)
{
isGrounded = true;
}
void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}