49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
public class BallController : MonoBehaviour {
|
|
float _ballSpeed;
|
|
float _brakeForce;
|
|
float _jumpForce;
|
|
Rigidbody _ball;
|
|
float outOfBounds = -1.8f;
|
|
|
|
void Start() {
|
|
_ball = GetComponent<Rigidbody>();
|
|
_ballSpeed = 3f;
|
|
_brakeForce = 0.3f;
|
|
_jumpForce = 14f;
|
|
}
|
|
|
|
void Update() {
|
|
KeyHandler();
|
|
CheckPlayerOut();
|
|
}
|
|
|
|
void CheckPlayerOut() {
|
|
if (transform.position.y < outOfBounds) {
|
|
transform.position = new Vector3(0, 1f, 0);
|
|
transform.rotation = Quaternion.Euler(0, 0, 0);
|
|
}
|
|
}
|
|
|
|
void KeyHandler() {
|
|
if (Keyboard.current.wKey.isPressed) {
|
|
_ball.AddForce(Vector3.forward * _ballSpeed);
|
|
}
|
|
else if (Keyboard.current.sKey.isPressed) {
|
|
_ball.AddForce(Vector3.back * _ballSpeed);
|
|
}
|
|
else if (Keyboard.current.dKey.isPressed) {
|
|
_ball.AddForce(Vector3.right * _ballSpeed);
|
|
}
|
|
else if (Keyboard.current.aKey.isPressed) {
|
|
_ball.AddForce(Vector3.left * _ballSpeed);
|
|
}
|
|
if (Keyboard.current.spaceKey.isPressed && transform.position.y < 1) {
|
|
_ball.AddForce(Vector3.up * _jumpForce);
|
|
}
|
|
else {
|
|
_ball.AddForce(-_ball.linearVelocity * _brakeForce);
|
|
}
|
|
}
|
|
} |