-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectDirectionalRotation.cs
More file actions
64 lines (52 loc) · 1.99 KB
/
DetectDirectionalRotation.cs
File metadata and controls
64 lines (52 loc) · 1.99 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
60
61
62
63
64
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectDirectionalRotation: MonoBehaviour
{
// This piece of script only show the methods to detect clockwise and anti-clockwise direction when you are rotating an object (or player)
// It is not a complete TPS locomotion script
// Camera
private Transform cam;
// Locomotion
private float RFrotationTurnVelocity;
private Vector3 direction;
// Locomotion States
public bool turnLeft; // Parameter on which anti-clockwise direction will be passed
public bool turnRight; // Parameter on which clockwise direction will be passed
private void Awake()
{
// Camera
cam = Camera.Main;
}
private void Update()
{
// Legacy Input System
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
direction = new Vector3(horizontal, 0, vertical).normalized;
// Movement
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref RFrotationTurnVelocity, rotationSmoothTime);
// Check if player is rotating clockwise or anti-clockwise
Orientation = angle;
// [Optional] Pass CLOCKWISE OR ANTI-CLOCKWISE to the booleans 'turnRight' and 'turnLeft'
// Examples : Use them for Banking in the Animator, Lookat in the direction the player is going and all sorts of fun locomotion features
turnRight = clockwiseRotation;
turnLeft = !clockwiseRotation;
}
// Check if player is rotating clockwise or anti-clockwise
private float _counter;
public bool clockwiseRotation;
private float Orientation
{
get
{
return _counter;
}
set
{
clockwiseRotation = _counter <= value;
_counter = value;
}
}
}