summaryrefslogtreecommitdiffhomepage
path: root/Assets/Scripts/MovingSphere.cs
blob: f92b4ea3d8a12aacf04bf35ca95dd0c184b79967 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingSphere : MonoBehaviour
{
	Rigidbody body;
	Renderer renderer;

	[SerializeField, Range(0f, 100f)]
	float maxSpeed = 10f;
	[SerializeField, Range(0f, 100f)]
	float maxAcceleration = 10f;
	[SerializeField, Range(0f, 100f)]
	float maxAirAcceleration = 1f;
	Vector3 velocity;

	[SerializeField, Range(0f, 10f)]
	float jumpHeight = 2f;
	[SerializeField, Range(0, 5)]
	int maxAirJumps;
	bool desiredJump;
	int jumpPhase;
	int groundContactCount;

	bool OnGround => groundContactCount > 0;

	Vector3 contactNormal;

	// 1.0 means walls
	// 0.0 means floors
	[SerializeField, Range(0f, 1f)]
	float maxJumpAngle = 0.44f; 

	Vector3 inputVelocity;

	void Awake()
	{
		body = GetComponent<Rigidbody>();
		renderer = GetComponent<Renderer>();
	}

	void Update()
	{
		Vector2 playerInput = new Vector2(
				Input.GetAxis("Horizontal"),
				Input.GetAxis("Vertical")
				);
		playerInput = Vector2.ClampMagnitude(playerInput, 1f);

		desiredJump |= Input.GetButtonDown("Jump");

		inputVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;

		Color purple = Color.red + Color.blue;
		renderer.material.SetColor(
				"_BaseColor", purple - (purple * (groundContactCount * 0.33f))
				);
	}

	void FixedUpdate()
	{
		UpdateState();
		AdjustVelocity();

		if(desiredJump)
		{
			desiredJump = false;
			Jump();
		}

		body.velocity = velocity;
		ClearState();
	}

	void ClearState() {
		groundContactCount = 0;
		contactNormal = Vector3.zero;
	}


	void UpdateState()
	{
		velocity = body.velocity;
		if(OnGround)
		{
			jumpPhase = 0;
			if(groundContactCount > 1)
				contactNormal.Normalize();
		}
		else
			contactNormal = Vector3.up;
	}

	void Jump()
	{
		if(OnGround || jumpPhase < maxAirJumps)
		{
			jumpPhase += 1;
			float jumpSpeed = Mathf.Sqrt(-2f * Physics.gravity.y * jumpHeight);
			float alignedSpeed = Vector3.Dot(velocity, contactNormal);
			if(alignedSpeed > 0f)
				jumpSpeed = Mathf.Max(jumpSpeed - alignedSpeed, 0f);
			velocity += contactNormal * jumpSpeed;
		}
	}

	void OnCollisionStay(Collision collision) {
		EvaluateCollision(collision);  
	}

	void OnCollisionEnter(Collision collision) {
		EvaluateCollision(collision);  
	}

	void EvaluateCollision(Collision collision) {
		for(int i = 0; i < collision.contactCount; i++)
		{
			Vector3 normal = collision.GetContact(i).normal;
			if(normal.y >= (1f - maxJumpAngle))
			{
				groundContactCount += 1;
				contactNormal += normal;
			}
		}
	}

	void AdjustVelocity()
	{
		Vector3 xAxis = ProjectOnContactPlane(Vector3.right).normalized;
		Vector3 zAxis = ProjectOnContactPlane(Vector3.forward).normalized;

		Vector3 currentVel = new Vector3(
				Vector3.Dot(velocity, xAxis),
				0f,
				Vector3.Dot(velocity, zAxis)
				);

		float maxSpeedChange = Time.deltaTime;
		if(OnGround)
			maxSpeedChange *= maxAcceleration;
		else
			maxSpeedChange *= maxAirAcceleration;

		Vector3 newVel = new Vector3(
				Mathf.MoveTowards(currentVel.x, inputVelocity.x, maxSpeedChange),
				0f,
				Mathf.MoveTowards(currentVel.z, inputVelocity.z, maxSpeedChange)
				);


		velocity += xAxis * (newVel.x - currentVel.x) + zAxis * (newVel.z - currentVel.z);
	}

	Vector3 ProjectOnContactPlane(Vector3 vector)
	{
		return vector - contactNormal * Vector3.Dot(vector, contactNormal);
	}
}