Skip to content

Instantly share code, notes, and snippets.

@gkagm2
Last active April 16, 2019 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkagm2/17bf3540450646dab7746dbe401906b2 to your computer and use it in GitHub Desktop.
Save gkagm2/17bf3540450646dab7746dbe401906b2 to your computer and use it in GitHub Desktop.
Character move
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를 통하여 움직인다.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment