Player only moves when the key is pressed, not when it's held.
Hello, I'm very new to Unity and C#. I'm making my player movement script for my game but I have a problem. When I press the key, so once it's pushed down, the player moves. But if I keep it held, it doesn't move anymore. So like in a flight sim (I am NOT trying to make a flight sim by the way) you would hold a key to go up, but for some reason with my game you have to spam it. Say I was using transform.translate for example, every time I pressed the key it would go up, not if I held it, but every time I pressed it. Can anyone help me please?
I am using the new unity input system too. It seems to me that that's what I should use since the old one would probably be removed at some point, so what's the point in learning old stuff right?
Here is my script (You will probably notice that I have an addforce in a fixed update, that's because I want the player to constantly move forward, I will probably change that to a transform.translate though so that the speed doesn't build up over time):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
Controls controls;
public float thrust;
void Awake(){
controls = new Controls();
controls.controlskeyboard.Up.started += ctx => Up();
controls.controlskeyboard.Down.started += ctx => Down();
}
void Up(){
rb.AddForce(Vector3.up * thrust);
}
void Down(){
rb.AddForce(Vector3.down * thrust);
}
void OnEnable(){
controls.controlskeyboard.Enable();
}
void OnDisable(){
controls.controlskeyboard.Disable();
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate(){
rb.AddForce(0, 0, 2000 * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Object rotates when moving sideways 0 Answers
How to move 3 players at the same time in V form,How to move 3 characters in the same time in V form 0 Answers
Moving simultaneously with UP LEFT, W D using GetAxisRaw. Rotating localScale to flip animation 2 Answers
Reduce Player Movement in Air 0 Answers
Dash Movement Platformer 0 Answers