-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovementWithJumping
More file actions
59 lines (42 loc) · 1.52 KB
/
Copy pathMovementWithJumping
File metadata and controls
59 lines (42 loc) · 1.52 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementWithJumping : MonoBehaviour
{
public float speed;
public float rotationSpeed;
public float jumpSpeed;
float originalSetpOffset;
float ySpeed;
private CharacterController characterController;
void Start()
{
characterController = GetComponent<CharacterController>();
originalSetpOffset = characterController.stepOffset;
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
float magnitude = Mathf.Clamp01(movementDirection.magnitude) * speed;
movementDirection.Normalize();
ySpeed += Physics.gravity.y * Time.deltaTime;
if (Input.GetButtonDown("Jump"))
{
characterController.stepOffset = originalSetpOffset;
ySpeed = jumpSpeed;
}
else
{
characterController.stepOffset = 0;
}
Vector3 velocity = movementDirection * magnitude;
velocity.y = ySpeed;
characterController.Move(velocity * Time.deltaTime);
if (movementDirection != Vector3.zero)
{
Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
}