Merhaba, Bugün 3 boyutlu bir karakteri klavyemizle nasıl kontrol edebileceğimize bakacağız.
Öncelikle objemize CharacterController componenti ekliyoruz.
1 2 |
float x = Input.GetAxis("Horizontal"); float z = Input.GetAxis("Vertical"); |
buradaki Horizontal ve Vertical kısımları benim bilgisayarımda W,A,S,D tuşlarıyla kontrol edilen değerler. Sizin farklı olabilir, bunu Unity’nin ayarlarından değiştirebilirsiniz
Yeni bir script oluşturup şunları yazalım.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public CharacterController characterController; public float jumpSpeed = 1.6f; public float speed = 6.0f; public float gravity = 9.81f; private Vector3 move = Vector3.zero; void Start() { characterController = GetComponent<CharacterController>(); } void Update() { float x = Input.GetAxis("Horizontal"); float z = Input.GetAxis("Vertical"); if (characterController.isGrounded) { move = transform.right * x + transform.forward * z; if (Input.GetButton("Jump")) { move.y = jumpSpeed; } } move.y -= gravity * Time.deltaTime; characterController.Move(move * speed * Time.deltaTime); } } |