I’m studying Unity and C# with reference book and I was making FPS game. I just follow book’s content about Player’s move script. Really, without a misspell. But If I see upward direction and go, The player goes up and fly like Minecraft creative mode. I do not know what is wrong but the move script is not wrong. I perfectly follow reference book. Please give me answer and I’m Korean and I don’t speak English very well. So I beg your pardon for the strange sentence.
public class PlayerMove : MonoBehaviour
{
public float moveSpeed = 7f;
CharacterController cc;
float gravity = -20f;
public float yVelocity = 0;
public float jumpPower = 10f;
// Start is called before the first frame update
private void Start()
{
cc = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//w, a, s, d 키를 누르면 입력하면 캐릭터를 그 방향으로 이동시키고 싶다.
//[spacebar] 키를 누르면 캐릭터를 수직으로 점프시키고 싶다.
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 dir = new Vector3(h, 0, v);
dir = dir.normalized;
dir = Camera.main.transform.TransformDirection(dir);
if (Input.GetButton("Jump"))
{
yVelocity = jumpPower;
}
yVelocity += gravity * Time.deltaTime;
dir.y = yVelocity;
cc.Move(dir * moveSpeed * Time.deltaTime);
//p = p0 + vt
transform.position += dir * moveSpeed * Time.deltaTime;
}
}