Unity 2.5D karaktär kontroll i plattform sidscrollande spel (11 / 12 steg)
Steg 11: Koden!
använder UnityEngine;
använder System.Collections;
allmän klass spelare: MonoBehaviour {
Animatör anim;
offentliga flyta smidigt = 1f;
offentliga flyta hastighet = 6.0F;
offentliga flyta jumpSpeed = 8.0F;
offentliga flyta gravitation = 20.0F;
privat Quaternion lookLeft;
privat Quaternion lookRight;
privat Vector3 moveDirection = Vector3.zero;
void Start() {
Cursor.Visible = false;
anim = GetComponent ();
Time.timeScale = 1;
lookRight = transform.rotation;
lookLeft = lookRight * Quaternion.Euler (0, 180, 0);
}
void Update() {
CharacterController controller = GetComponent();
om (controller.isGrounded) {
anim. SetBool ("IsRunning", falsk);
moveDirection = ny Vector3(-(Input.GetAxis("Vertical")), 0, Input.GetAxis("Horizontal"));
om (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
om (Input.GetKey(KeyCode.A)) {
Transform.rotation = lookLeft;
moveDirection = transformering. TransformDirection(-moveDirection);
moveDirection * = hastighet;
anim. SetBool ("IsRunning", true);
}
om (Input.GetKey(KeyCode.D)) {
Transform.rotation = lookRight;
moveDirection = transformering. TransformDirection(moveDirection);
moveDirection * = hastighet;
anim. SetBool ("IsRunning", true);
}
}
moveDirection.y-= gravitation * Time.deltaTime;
Controller. Move(moveDirection * Time.deltaTime);
}
}