I have noticed that my player object, the green sphere, will slowly fall up and down. When the space bar is pressed, the player is supposed to jump and the fall. But my player just vibrates up and down. If i turn off the gravity component of rigidbody then my player does not do the slow fall but still vibrates fast on space press.
public class PlayerMovement : MonoBehaviour
{
private Rigidbody rb;
private bool hasJumped;
public int playerHealth;
public Vector3 safeSpot;
public int speed;
public float velocity;
public float jumpforce = 20;
public float gravity = -9.18f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
hasJumped = false;
}
private void FixedUpdate()
{
float xMove = Input.GetAxisRaw("Horizontal");
float zMove = Input.GetAxisRaw("Vertical");
if (Input.GetKey(KeyCode.Space) && !hasJumped)
{
rb.AddForce(Vector2.up * jumpforce);
hasJumped = true;
}
else if (hasJumped && rb.velocity.y !=0)
{
rb.AddForce(Vector2.down * jumpforce);
}
if(rb.velocity.y <= -9)
{
rb.velocity.y.Equals(0);
}
rb.velocity = new Vector3(xMove, 0, 0) * speed * Time.deltaTime;
}
public void damagePlayer(int damageType)
{
if(playerHealth > 0)
{
playerHealth -= damageType;
this.transform.position = safeSpot;
this.transform.position = new Vector3(this.transform.position.x - 5, this.transform.position.y,
this.transform.position.z);
}
else
{
//Game Over
}
}
}