-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding controls for the example scene, building new unitypackage and …
…setting the default vertex count to Low so it loads on Windows
- Loading branch information
1 parent
caf31b7
commit 5f7d1a5
Showing
12 changed files
with
695 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// original by Eric Haines (Eric5h5) | ||
// adapted by @torahhorse | ||
// http://wiki.unity3d.com/index.php/FPSWalkerEnhanced | ||
|
||
using UnityEngine; | ||
using System.Collections; | ||
|
||
[RequireComponent (typeof (CharacterController))] | ||
public class FirstPersonDrifter: MonoBehaviour | ||
{ | ||
public float walkSpeed = 6.0f; | ||
public float runSpeed = 10.0f; | ||
|
||
// If true, diagonal speed (when strafing + moving forward or back) can't exceed normal move speed; otherwise it's about 1.4 times faster | ||
private bool limitDiagonalSpeed = true; | ||
|
||
public bool enableRunning = false; | ||
|
||
public float jumpSpeed = 4.0f; | ||
public float gravity = 10.0f; | ||
|
||
// Units that player can fall before a falling damage function is run. To disable, type "infinity" in the inspector | ||
private float fallingDamageThreshold = 10.0f; | ||
|
||
// If the player ends up on a slope which is at least the Slope Limit as set on the character controller, then he will slide down | ||
public bool slideWhenOverSlopeLimit = false; | ||
|
||
// If checked and the player is on an object tagged "Slide", he will slide down it regardless of the slope limit | ||
public bool slideOnTaggedObjects = false; | ||
|
||
public float slideSpeed = 5.0f; | ||
|
||
// If checked, then the player can change direction while in the air | ||
public bool airControl = true; | ||
|
||
// Small amounts of this results in bumping when walking down slopes, but large amounts results in falling too fast | ||
public float antiBumpFactor = .75f; | ||
|
||
// Player must be grounded for at least this many physics frames before being able to jump again; set to 0 to allow bunny hopping | ||
public int antiBunnyHopFactor = 1; | ||
|
||
private Vector3 moveDirection = Vector3.zero; | ||
private bool grounded = false; | ||
private CharacterController controller; | ||
private Transform myTransform; | ||
private float speed; | ||
private RaycastHit hit; | ||
private float fallStartLevel; | ||
private bool falling; | ||
private float slideLimit; | ||
private float rayDistance; | ||
private Vector3 contactPoint; | ||
private bool playerControl = false; | ||
private int jumpTimer; | ||
|
||
void Start() | ||
{ | ||
controller = GetComponent<CharacterController>(); | ||
myTransform = transform; | ||
speed = walkSpeed; | ||
rayDistance = controller.height * .5f + controller.radius; | ||
slideLimit = controller.slopeLimit - .1f; | ||
jumpTimer = antiBunnyHopFactor; | ||
} | ||
|
||
void FixedUpdate() { | ||
float inputX = Input.GetAxis("Horizontal"); | ||
float inputY = Input.GetAxis("Vertical"); | ||
// If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed | ||
float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f && limitDiagonalSpeed)? .7071f : 1.0f; | ||
|
||
if (grounded) { | ||
bool sliding = false; | ||
// See if surface immediately below should be slid down. We use this normally rather than a ControllerColliderHit point, | ||
// because that interferes with step climbing amongst other annoyances | ||
if (Physics.Raycast(myTransform.position, -Vector3.up, out hit, rayDistance)) { | ||
if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit) | ||
sliding = true; | ||
} | ||
// However, just raycasting straight down from the center can fail when on steep slopes | ||
// So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead | ||
else { | ||
Physics.Raycast(contactPoint + Vector3.up, -Vector3.up, out hit); | ||
if (Vector3.Angle(hit.normal, Vector3.up) > slideLimit) | ||
sliding = true; | ||
} | ||
|
||
// If we were falling, and we fell a vertical distance greater than the threshold, run a falling damage routine | ||
if (falling) { | ||
falling = false; | ||
if (myTransform.position.y < fallStartLevel - fallingDamageThreshold) | ||
FallingDamageAlert (fallStartLevel - myTransform.position.y); | ||
} | ||
|
||
if( enableRunning ) | ||
{ | ||
speed = Input.GetButton("Run")? runSpeed : walkSpeed; | ||
} | ||
|
||
// If sliding (and it's allowed), or if we're on an object tagged "Slide", get a vector pointing down the slope we're on | ||
if ( (sliding && slideWhenOverSlopeLimit) || (slideOnTaggedObjects && hit.collider.tag == "Slide") ) { | ||
Vector3 hitNormal = hit.normal; | ||
moveDirection = new Vector3(hitNormal.x, -hitNormal.y, hitNormal.z); | ||
Vector3.OrthoNormalize (ref hitNormal, ref moveDirection); | ||
moveDirection *= slideSpeed; | ||
playerControl = false; | ||
} | ||
// Otherwise recalculate moveDirection directly from axes, adding a bit of -y to avoid bumping down inclines | ||
else { | ||
moveDirection = new Vector3(inputX * inputModifyFactor, -antiBumpFactor, inputY * inputModifyFactor); | ||
moveDirection = myTransform.TransformDirection(moveDirection) * speed; | ||
playerControl = true; | ||
} | ||
|
||
// Jump! But only if the jump button has been released and player has been grounded for a given number of frames | ||
if (!Input.GetButton("Jump")) | ||
jumpTimer++; | ||
else if (jumpTimer >= antiBunnyHopFactor) { | ||
moveDirection.y = jumpSpeed; | ||
jumpTimer = 0; | ||
} | ||
} | ||
else { | ||
// If we stepped over a cliff or something, set the height at which we started falling | ||
if (!falling) { | ||
falling = true; | ||
fallStartLevel = myTransform.position.y; | ||
} | ||
|
||
// If air control is allowed, check movement but don't touch the y component | ||
if (airControl && playerControl) { | ||
moveDirection.x = inputX * speed * inputModifyFactor; | ||
moveDirection.z = inputY * speed * inputModifyFactor; | ||
moveDirection = myTransform.TransformDirection(moveDirection); | ||
} | ||
} | ||
|
||
// Apply gravity | ||
moveDirection.y -= gravity * Time.deltaTime; | ||
|
||
// Move the controller, and set grounded true or false depending on whether we're standing on something | ||
grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0; | ||
} | ||
|
||
// Store point that we're in contact with for use in FixedUpdate if needed | ||
void OnControllerColliderHit (ControllerColliderHit hit) { | ||
contactPoint = hit.point; | ||
} | ||
|
||
// If falling damage occured, this is the place to do something about it. You can make the player | ||
// have hitpoints and remove some of them based on the distance fallen, add sound effects, etc. | ||
void FallingDamageAlert (float fallDistance) | ||
{ | ||
//print ("Ouch! Fell " + fallDistance + " units!"); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// original by asteins | ||
// adapted by @torahhorse | ||
// http://wiki.unity3d.com/index.php/SmoothMouseLook | ||
|
||
// Instructions: | ||
// There should be one MouseLook script on the Player itself, and another on the camera | ||
// player's MouseLook should use MouseX, camera's MouseLook should use MouseY | ||
|
||
using UnityEngine; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
public class MouseLook : MonoBehaviour | ||
{ | ||
|
||
public enum RotationAxes { MouseX = 1, MouseY = 2 } | ||
public RotationAxes axes = RotationAxes.MouseX; | ||
public bool invertY = false; | ||
|
||
public float sensitivityX = 10F; | ||
public float sensitivityY = 9F; | ||
|
||
public float minimumX = -360F; | ||
public float maximumX = 360F; | ||
|
||
public float minimumY = -85F; | ||
public float maximumY = 85F; | ||
|
||
float rotationX = 0F; | ||
float rotationY = 0F; | ||
|
||
private List<float> rotArrayX = new List<float>(); | ||
float rotAverageX = 0F; | ||
|
||
private List<float> rotArrayY = new List<float>(); | ||
float rotAverageY = 0F; | ||
|
||
public float framesOfSmoothing = 5; | ||
|
||
Quaternion originalRotation; | ||
|
||
void Start () | ||
{ | ||
if (GetComponent<Rigidbody>()) | ||
{ | ||
GetComponent<Rigidbody>().freezeRotation = true; | ||
} | ||
|
||
originalRotation = transform.localRotation; | ||
} | ||
|
||
void Update () | ||
{ | ||
if (axes == RotationAxes.MouseX) | ||
{ | ||
rotAverageX = 0f; | ||
|
||
rotationX += Input.GetAxis("Mouse X") * sensitivityX * Time.timeScale; | ||
|
||
rotArrayX.Add(rotationX); | ||
|
||
if (rotArrayX.Count >= framesOfSmoothing) | ||
{ | ||
rotArrayX.RemoveAt(0); | ||
} | ||
for(int i = 0; i < rotArrayX.Count; i++) | ||
{ | ||
rotAverageX += rotArrayX[i]; | ||
} | ||
rotAverageX /= rotArrayX.Count; | ||
rotAverageX = ClampAngle(rotAverageX, minimumX, maximumX); | ||
|
||
Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up); | ||
transform.localRotation = originalRotation * xQuaternion; | ||
} | ||
else | ||
{ | ||
rotAverageY = 0f; | ||
|
||
float invertFlag = 1f; | ||
if( invertY ) | ||
{ | ||
invertFlag = -1f; | ||
} | ||
rotationY += Input.GetAxis("Mouse Y") * sensitivityY * invertFlag * Time.timeScale; | ||
|
||
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY); | ||
|
||
rotArrayY.Add(rotationY); | ||
|
||
if (rotArrayY.Count >= framesOfSmoothing) | ||
{ | ||
rotArrayY.RemoveAt(0); | ||
} | ||
for(int j = 0; j < rotArrayY.Count; j++) | ||
{ | ||
rotAverageY += rotArrayY[j]; | ||
} | ||
rotAverageY /= rotArrayY.Count; | ||
|
||
Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left); | ||
transform.localRotation = originalRotation * yQuaternion; | ||
} | ||
} | ||
|
||
public void SetSensitivity(float s) | ||
{ | ||
sensitivityX = s; | ||
sensitivityY = s; | ||
} | ||
|
||
public static float ClampAngle (float angle, float min, float max) | ||
{ | ||
angle = angle % 360; | ||
if ((angle >= -360F) && (angle <= 360F)) { | ||
if (angle < -360F) { | ||
angle += 360F; | ||
} | ||
if (angle > 360F) { | ||
angle -= 360F; | ||
} | ||
} | ||
return Mathf.Clamp (angle, min, max); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.