The question is answered, right answer was accepted
why is my my movement not working?,why does my addforce only last for a split second?
im very new to coding so sry if this is a simple mistake.
ive set up my cube to move in any direction but for some reason whenever i press a button to move i move in the direction i pressed but only for a split second even if i hold the key down. here is my script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ri : MonoBehaviour {
private Rigidbody rb;
public int speed;
public int hspeed;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("w"))
{
rb.AddForce(0, 0, speed);
}
if (Input.GetKeyDown("s"))
{
rb.AddForce(0, 0, -speed);
}
if (Input.GetKeyDown("a"))
{
rb.AddForce(hspeed, 0, 0);
}
if (Input.GetKeyDown("d"))
{
rb.AddForce(-hspeed, 0, 0);
}
}
} ,im very new to coding and am trying to get a block to move around and ive set it up so that it can move in any direction but whenever i press the key to move in that direction the cube moves for like half a second then stops. the only way to move it is to key pressing the key. here is my code.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ri : MonoBehaviour {
private Rigidbody rb;
public int speed;
public int hspeed;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("w"))
{
rb.AddForce(0, 0, speed);
}
if (Input.GetKeyDown("s"))
{
rb.AddForce(0, 0, -speed);
}
if (Input.GetKeyDown("a"))
{
rb.AddForce(hspeed, 0, 0);
}
if (Input.GetKeyDown("d"))
{
rb.AddForce(-hspeed, 0, 0);
}
}
}