-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicMovement.cs
75 lines (52 loc) · 1.7 KB
/
BasicMovement.cs
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
60
61
62
63
64
65
66
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Script per il movimento in avanti e indietro di un 3D Object
//Creo un oggetto ad esempio un cubo, aggiungo questo script,
//e trascino la MainCamera all'interno dell'oggetto
//Non è implementato il salto dell'oggetto
public class BasicMovement : MonoBehaviour
{
float speed = 15;
float rotationSpeed = 150;
float rotation = 0f;
float gravity = 9.81f;
Vector3 moveDirection = Vector3.zero;
CharacterController controller;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()
{
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.UpArrow)) // Avanti
{
moveDirection = new Vector3(0, 0, 1);
moveDirection *= speed;
moveDirection = transform.TransformDirection(moveDirection);
}
else if (Input.GetKey(KeyCode.DownArrow)) // Indietro
{
moveDirection = new Vector3(0, 0, 1);
moveDirection *= speed;
moveDirection = transform.TransformDirection(-moveDirection);
}
else
{
moveDirection = new Vector3(0, 0, 0);
}
}
rotation += Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rotation, 0);
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}