UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"Complex Character Controller that uses RigidBody"
using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class ComplexCharacterController : MonoBehaviour { public float moveSpeed = 5f; public float rotationSpeed = 100f; public float jumpForce = 5f; private Rigidbody rb; private bool isGrounded; void Start() { rb = GetComponent<Rigidbody>(); } void Update() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(horizontal, 0, vertical); rb.MovePosition(transform.position + movement * moveSpeed * Time.deltaTime); if (Input.GetKey(KeyCode.Q)) { transform.Rotate(Vector3.down * rotationSpeed * Time.deltaTime); } if (Input.GetKey(KeyCode.E)) { transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime); } if (Input.GetButtonDown("Jump") && isGrounded) { rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); isGrounded = false; } } private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; } } }