This repository has been archived on 2025-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
unity-cube-game/Assets/Scripts/PlayerMovement.cs

38 lines
827 B
C#

using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float speed=500;
public float hSpeed=200;
bool mvLeft, mvRight = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
mvRight=Input.GetKey("d");
mvLeft=Input.GetKey("a");
}
void FixedUpdate() {
rb.AddForce(0,0,speed * Time.deltaTime);
if (mvRight) {
rb.AddForce(hSpeed * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (mvLeft) {
rb.AddForce(-hSpeed * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f) {
FindObjectOfType<GameManager>().EndGame();
}
}
}