-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
66 lines (53 loc) · 2.28 KB
/
PlayerController.cs
File metadata and controls
66 lines (53 loc) · 2.28 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
65
66
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerController : MonoBehaviour {
[Header ("Speed")]
[Tooltip("In ms^-1")][SerializeField] float xSpeed = 4f;
[Tooltip("In ms^-1")] [SerializeField] float ySpeed = 4f;
[Header("Screen-position Based")]
[SerializeField] float screenEdge = 5f;
[SerializeField] float yMin = -4f;
[SerializeField] float yMax = 2f;
[SerializeField] float positionPitchFactor = -5f;
[SerializeField] float positionYawFactor = 5f;
[Header("Control-Throw Based")]
[SerializeField] float controlRollFactor = 18f;
[SerializeField] float controlPitchFactor = -5f;
float xThrow, yThrow;
bool playerDead = false;
void Update () {
if (!playerDead)
{
ProcessTranslation();
ProcessRotation();
}
}
private void ProcessTranslation()
{
xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
float xOffset = xThrow * xSpeed * Time.deltaTime;
float rawXPos = transform.localPosition.x + xOffset;
transform.localPosition = new Vector3(Mathf.Clamp(rawXPos, -screenEdge, screenEdge), transform.localPosition.y, transform.localPosition.z);
yThrow = CrossPlatformInputManager.GetAxis("Vertical");
float yOffset = yThrow * ySpeed * Time.deltaTime;
float rawYPos = transform.localPosition.y + yOffset;
transform.localPosition = new Vector3(transform.localPosition.x, Mathf.Clamp(rawYPos, yMin, yMax), transform.localPosition.z);
}
private void ProcessRotation()
{
float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
float pitchDueToControl = yThrow * controlPitchFactor;
float pitch = pitchDueToPosition + pitchDueToControl;
float yawDueToPosition = transform.localPosition.x * positionYawFactor;
float yaw= yawDueToPosition;
float rollDueToControl = xThrow * controlRollFactor;
float roll=rollDueToControl;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
}
void KillMovement() // Called by string reference
{
playerDead = true;
}
}