Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CharacterMove : MonoBehaviour {
- public Transform cameraTransform;
- public float moveSpeed = 10.0f;
- public float jumpSpeed = 10.0f;
- public float gravity = -20.0f;
- CharacterController characterController = null;
- float yVelocity = 0.0f;
- // Use this for initialization
- void Start () {
- characterController = GetComponent<CharacterController>();
- }
- // Update is called once per frame
- void Update()
- {
- float x = Input.GetAxis("Horizontal");
- float z = Input.GetAxis("Vertical");
- Vector3 moveDirection = new Vector3(x, 0, z);
- moveDirection = cameraTransform.TransformDirection(moveDirection); //월드좌표계로 변환하여 moveDirection으로 넣음.
- moveDirection *= moveSpeed;
- if (characterController.isGrounded)
- {
- yVelocity = 0.0f;
- if (Input.GetButtonDown("Jump")) //점프면
- {
- yVelocity = jumpSpeed; //점프 스피드를 y속도값에 넣음
- }
- }
- yVelocity += (gravity * Time.deltaTime); //중력에 의하여 점점 스피드가 줄어듬.
- moveDirection.y = yVelocity; //y값에 y속도값을 넣는다.
- characterController.Move(moveDirection * Time.deltaTime); //컨트롤러.Move를 통하여 움직인다.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement