Merge pull request #16 from gjcampbell777/add_momentum_to_movement
Add momentum to movement
This commit is contained in:
@@ -1284,7 +1284,10 @@ MonoBehaviour:
|
|||||||
m_Script: {fileID: 11500000, guid: 83127dd120c01de45954b99cc4d669a5, type: 3}
|
m_Script: {fileID: 11500000, guid: 83127dd120c01de45954b99cc4d669a5, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
speed: 20
|
speed: 10
|
||||||
|
maxSpeed: 25
|
||||||
|
acceleration: 3
|
||||||
|
friction: 0.1
|
||||||
jumpHeight: 20
|
jumpHeight: 20
|
||||||
gravity: 5
|
gravity: 5
|
||||||
rotationSpeed: 20
|
rotationSpeed: 20
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ public class PlayerController : MonoBehaviour
|
|||||||
CapsuleCollider capsule;
|
CapsuleCollider capsule;
|
||||||
|
|
||||||
public float speed;
|
public float speed;
|
||||||
|
public float maxSpeed;
|
||||||
|
public float acceleration;
|
||||||
|
public float friction;
|
||||||
public float jumpHeight;
|
public float jumpHeight;
|
||||||
public float gravity;
|
public float gravity;
|
||||||
public float rotationSpeed;
|
public float rotationSpeed;
|
||||||
@@ -20,10 +23,14 @@ public class PlayerController : MonoBehaviour
|
|||||||
public GameObject playerModel;
|
public GameObject playerModel;
|
||||||
|
|
||||||
private int jump = 0;
|
private int jump = 0;
|
||||||
|
private float maxSpeedStore;
|
||||||
private float capsuleHeight;
|
private float capsuleHeight;
|
||||||
private float controllerHeight;
|
private float controllerHeight;
|
||||||
private float transformHeight;
|
private float transformHeight;
|
||||||
|
private float xVelocity = 0.0f;
|
||||||
|
private float zVelocity = 0.0f;
|
||||||
private Vector3 moveDirection;
|
private Vector3 moveDirection;
|
||||||
|
private Vector3 velocity;
|
||||||
|
|
||||||
float startTime = 0.0f;
|
float startTime = 0.0f;
|
||||||
float oneSec = 1.0f;
|
float oneSec = 1.0f;
|
||||||
@@ -35,20 +42,31 @@ public class PlayerController : MonoBehaviour
|
|||||||
transformHeight = transform.localScale.y;
|
transformHeight = transform.localScale.y;
|
||||||
controllerHeight = characterController.height;
|
controllerHeight = characterController.height;
|
||||||
capsuleHeight = capsule.height;
|
capsuleHeight = capsule.height;
|
||||||
|
maxSpeedStore = maxSpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
|
|
||||||
float yStore = moveDirection.y;
|
float yStore = moveDirection.y;
|
||||||
|
maxSpeed = maxSpeedStore;
|
||||||
//Need to switch to 'raw' when using keyboard
|
//Need to switch to 'raw' when using keyboard
|
||||||
moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
|
//moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
|
||||||
|
moveDirection = new Vector3(Input.GetAxis("Horizontal"), moveDirection.y, Input.GetAxis("Vertical"));
|
||||||
|
moveDirection = transform.TransformDirection(moveDirection);
|
||||||
|
moveDirection = Vector3.ClampMagnitude(moveDirection, 1.0f);
|
||||||
|
|
||||||
|
velocity.x += moveDirection.x * acceleration;
|
||||||
|
velocity.z += moveDirection.z * acceleration;
|
||||||
|
|
||||||
if (jump <= 1)
|
if (jump <= 1)
|
||||||
{
|
{
|
||||||
moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run
|
//moveDirection = moveDirection.normalized * speed; //Remove this line to make running diagonal the fastest standard run
|
||||||
|
maxSpeed = maxSpeedStore;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
moveDirection = (moveDirection.normalized * speed)/4; //Remove this line to make running diagonal the fastest standard run
|
//moveDirection = (moveDirection.normalized * speed)/4; //Remove this line to make running diagonal the fastest standard run
|
||||||
|
maxSpeed = maxSpeedStore/2;
|
||||||
}
|
}
|
||||||
|
|
||||||
moveDirection.y = yStore;
|
moveDirection.y = yStore;
|
||||||
@@ -60,9 +78,11 @@ public class PlayerController : MonoBehaviour
|
|||||||
|
|
||||||
if(Input.GetKey(KeyCode.LeftShift))
|
if(Input.GetKey(KeyCode.LeftShift))
|
||||||
{
|
{
|
||||||
moveDirection = (moveDirection.normalized * speed/2);
|
//moveDirection = (moveDirection.normalized * speed/2);
|
||||||
|
maxSpeed = maxSpeedStore/2;
|
||||||
} else {
|
} else {
|
||||||
moveDirection = moveDirection.normalized * speed;
|
//moveDirection = moveDirection.normalized * speed;
|
||||||
|
maxSpeed = maxSpeedStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Input.GetKey(KeyCode.LeftControl))
|
if(Input.GetKey(KeyCode.LeftControl))
|
||||||
@@ -72,7 +92,8 @@ public class PlayerController : MonoBehaviour
|
|||||||
capsule.height /= 2;
|
capsule.height /= 2;
|
||||||
transform.localScale = new Vector3(transform.localScale.x, transformHeight/2, transform.localScale.z);
|
transform.localScale = new Vector3(transform.localScale.x, transformHeight/2, transform.localScale.z);
|
||||||
|
|
||||||
if(Input.GetKeyDown(KeyCode.LeftControl) && characterController.velocity != new Vector3(0, 0, 0))
|
if(Input.GetKeyDown(KeyCode.LeftControl) && characterController.velocity != new Vector3(0, 0, 0)
|
||||||
|
&& (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
|
||||||
{
|
{
|
||||||
startTime = Time.time;
|
startTime = Time.time;
|
||||||
}
|
}
|
||||||
@@ -80,11 +101,13 @@ public class PlayerController : MonoBehaviour
|
|||||||
if(startTime + oneSec >= Time.time)
|
if(startTime + oneSec >= Time.time)
|
||||||
{
|
{
|
||||||
|
|
||||||
moveDirection = (moveDirection.normalized * speed * 2);
|
//moveDirection = (moveDirection.normalized * speed * 2);
|
||||||
|
maxSpeed = maxSpeedStore*2;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
moveDirection = (moveDirection.normalized * speed/4);
|
//moveDirection = (moveDirection.normalized * speed/4);
|
||||||
|
maxSpeed = maxSpeedStore/4;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +120,13 @@ public class PlayerController : MonoBehaviour
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0){
|
||||||
|
|
||||||
|
velocity.x = Mathf.SmoothDamp(velocity.x, 0.0f, ref xVelocity, friction);
|
||||||
|
velocity.z = Mathf.SmoothDamp(velocity.z, 0.0f, ref zVelocity, friction);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (Input.GetButtonDown("Jump") && jump <= 1)
|
if (Input.GetButtonDown("Jump") && jump <= 1)
|
||||||
{
|
{
|
||||||
moveDirection.y = jumpHeight;
|
moveDirection.y = jumpHeight;
|
||||||
@@ -105,11 +135,14 @@ public class PlayerController : MonoBehaviour
|
|||||||
|
|
||||||
moveDirection.y += Physics.gravity.y * gravity * Time.deltaTime;
|
moveDirection.y += Physics.gravity.y * gravity * Time.deltaTime;
|
||||||
|
|
||||||
|
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
|
||||||
|
|
||||||
|
velocity.y = moveDirection.y;
|
||||||
|
|
||||||
// Move the controller
|
// Move the controller
|
||||||
characterController.Move(moveDirection * Time.deltaTime);
|
characterController.Move(velocity * Time.deltaTime);
|
||||||
|
|
||||||
//Move the player in different directions based on camera look direction
|
//Move the player in different directions based on camera look direction
|
||||||
//Need to switch to 'raw' when using keyboard
|
|
||||||
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
|
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
|
||||||
{
|
{
|
||||||
transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
|
transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
|
||||||
|
|||||||
Reference in New Issue
Block a user