6 Kickass Unity Assets for Your Leap Motion VR Projects. Share Tweet LinkedIn. Avatar Hand Controller for Leap Motion – $5. One of many physics-based assets that can be combined with Leap Motion to create physics-fueled fun that’s impossible with standard controllers.
This script is attached to the Character Controller or your player in your game. This script enables your player to crouch using the CTRL key and run using the SHIFT key. Simply attach it to your character controller and follow the instructions in red color.
Attach this to your Character controller and remove these scripts:
- Character Motor
-FPSinputController
-FPSinputController
Here is the JAVASCRIPT .Js:
var walkSpeed =6.0;
var runSpeed =11.0;
// If true, diagonal speed (when strafing + moving forward or back) can't exceed normal move speed; otherwise it's about 1.4 times faster
var limitDiagonalSpeed =true;
var limitDiagonalSpeed =true;
// If checked, the run key toggles between running and walking. Otherwise player runs if the key is held down and walks otherwise
// There must be a button set up in the Input Manager called 'Run'
var toggleRun =false;
// There must be a button set up in the Input Manager called 'Run'
var toggleRun =false;
var jumpSpeed =8.0;
var gravity =20.0;
var gravity =20.0;
// Units that player can fall before a falling damage function is run. To disable, type 'infinity' in the inspector
var fallingDamageThreshold =10.0;
var fallingDamageThreshold =10.0;
// 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
var slideWhenOverSlopeLimit =false;
var slideWhenOverSlopeLimit =false;
// If checked and the player is on an object tagged 'Slide', he will slide down it regardless of the slope limit
var slideOnTaggedObjects =false;
var slideOnTaggedObjects =false;
var slideSpeed =12.0;
// If checked, then the player can change direction while in the air
var airControl =false;
var airControl =false;
// Small amounts of this results in bumping when walking down slopes, but large amounts results in falling too fast
var antiBumpFactor = .75;
var antiBumpFactor = .75;
// Player must be grounded for at least this many physics frames before being able to jump again; set to 0 to allow bunny hopping
var antiBunnyHopFactor =1;
var antiBunnyHopFactor =1;
Best Physics Based Fighting Games
privatevar moveDirection = Vector3.zero;
privatevar grounded =false;
privatevar controller : CharacterController;
privatevar myTransform : Transform;
privatevar speed : float;
privatevar hit : RaycastHit;
privatevar fallStartLevel : float;
privatevar falling =false;
privatevar slideLimit : float;
privatevar rayDistance : float;
privatevar contactPoint : Vector3;
privatevar playerControl =false;
privatevar jumpTimer : int;
privatevar grounded =false;
privatevar controller : CharacterController;
privatevar myTransform : Transform;
privatevar speed : float;
privatevar hit : RaycastHit;
privatevar fallStartLevel : float;
privatevar falling =false;
privatevar slideLimit : float;
privatevar rayDistance : float;
privatevar contactPoint : Vector3;
privatevar playerControl =false;
privatevar jumpTimer : int;
function Start (){
controller = GetComponent(CharacterController);
myTransform = transform;
speed = walkSpeed;
rayDistance = controller.height* .5 + controller.radius;
slideLimit = controller.slopeLimit- .1;
jumpTimer = antiBunnyHopFactor;
oldPos = transform.position;
}
controller = GetComponent(CharacterController);
myTransform = transform;
speed = walkSpeed;
rayDistance = controller.height* .5 + controller.radius;
slideLimit = controller.slopeLimit- .1;
jumpTimer = antiBunnyHopFactor;
oldPos = transform.position;
}
function FixedUpdate(){
var inputX = Input.GetAxis('Horizontal');
var 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
var inputModifyFactor =(inputX !=0.0&& inputY !=0.0&& limitDiagonalSpeed)? .7071 :1.0;
var inputX = Input.GetAxis('Horizontal');
var 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
var inputModifyFactor =(inputX !=0.0&& inputY !=0.0&& limitDiagonalSpeed)? .7071 :1.0;
if(grounded){
var 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, 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, hit);
if(Vector3.Angle(hit.normal, Vector3.up)> slideLimit)
sliding =true;
}
var 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, 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, 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(falling){
falling =false;
if(myTransform.position.y< fallStartLevel - fallingDamageThreshold)
FallingDamageAlert (fallStartLevel - myTransform.position.y);
}
// If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down
if(!toggleRun)
speed = Input.GetButton('Run')? runSpeed : walkSpeed;
if(!toggleRun)
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')){
var hitNormal = hit.normal;
moveDirection = Vector3(hitNormal.x,-hitNormal.y, hitNormal.z);
Vector3.OrthoNormalize(hitNormal, moveDirection);
moveDirection *= slideSpeed;
playerControl =false;
}
// Otherwise recalculate moveDirection directly from axes, adding a bit of -y to avoid bumping down inclines
else{
moveDirection = Vector3(inputX * inputModifyFactor,-antiBumpFactor, inputY * inputModifyFactor);
moveDirection = myTransform.TransformDirection(moveDirection)* speed;
playerControl =true;
}
if((sliding && slideWhenOverSlopeLimit)||(slideOnTaggedObjects && hit.collider.tag'Slide')){
var hitNormal = hit.normal;
moveDirection = Vector3(hitNormal.x,-hitNormal.y, hitNormal.z);
Vector3.OrthoNormalize(hitNormal, moveDirection);
moveDirection *= slideSpeed;
playerControl =false;
}
// Otherwise recalculate moveDirection directly from axes, adding a bit of -y to avoid bumping down inclines
else{
moveDirection = 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++;
elseif(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(!Input.GetButton('Jump'))
jumpTimer++;
elseif(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);
}
}
if(airControl && playerControl){
moveDirection.x= inputX * speed * inputModifyFactor;
moveDirection.z= inputY * speed * inputModifyFactor;
moveDirection = myTransform.TransformDirection(moveDirection);
}
}
// Apply gravity
moveDirection.y-= gravity * Time.deltaTime;
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;
}
grounded =(controller.Move(moveDirection * Time.deltaTime)& CollisionFlags.Below)!=0;
}
function Update (){
// If the run button is set to toggle, then switch between walk/run speed. (We use Update for this..
// FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
if(toggleRun && grounded && Input.GetButtonDown('Run'))
speed =(speed walkSpeed? runSpeed : walkSpeed);
}
// If the run button is set to toggle, then switch between walk/run speed. (We use Update for this..
// FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
if(toggleRun && grounded && Input.GetButtonDown('Run'))
speed =(speed walkSpeed? runSpeed : walkSpeed);
}
// Store point that we're in contact with for use in FixedUpdate if needed
function OnControllerColliderHit (hit : ControllerColliderHit){
contactPoint = hit.point;
}
function OnControllerColliderHit (hit : ControllerColliderHit){
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.
function FallingDamageAlert (fallDistance : float){
Debug.Log('Ouch! Fell '+ fallDistance +' units!');
}
// have hitpoints and remove some of them based on the distance fallen, add sound effects, etc.
function FallingDamageAlert (fallDistance : float){
Debug.Log('Ouch! Fell '+ fallDistance +' units!');
}
@script RequireComponent(CharacterController)
Here is the C# script .cs:
using UnityEngine;using System.Collections;
[RequireComponent (typeof(CharacterController))]
publicclass FPSWalkerEnhanced: MonoBehaviour {
publicclass FPSWalkerEnhanced: MonoBehaviour {
public float walkSpeed = 6.0f;
public float runSpeed = 11.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
public bool limitDiagonalSpeed =true;
public bool limitDiagonalSpeed =true;
// If checked, the run key toggles between running and walking. Otherwise player runs if the key is held down and walks otherwise
// There must be a button set up in the Input Manager called 'Run'
public bool toggleRun =false;
// There must be a button set up in the Input Manager called 'Run'
public bool toggleRun =false;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float gravity = 20.0f;
// Units that player can fall before a falling damage function is run. To disable, type 'infinity' in the inspector
public float fallingDamageThreshold = 10.0f;
public 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;
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 bool slideOnTaggedObjects =false;
public float slideSpeed = 12.0f;
// If checked, then the player can change direction while in the air
public bool airControl =false;
public bool airControl =false;
// Small amounts of this results in bumping when walking down slopes, but large amounts results in falling too fast
public float antiBumpFactor = .75f;
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;
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;
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;
}
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;
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;
}
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(falling){
falling =false;
if(myTransform.position.y< fallStartLevel - fallingDamageThreshold)
FallingDamageAlert (fallStartLevel - myTransform.position.y);
}
// If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down
if(!toggleRun)
speed = Input.GetButton('Run')? runSpeed : walkSpeed;
if(!toggleRun)
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;
}
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++;
elseif(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(!Input.GetButton('Jump'))
jumpTimer++;
elseif(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);
}
}
if(airControl && playerControl){
moveDirection.x= inputX * speed * inputModifyFactor;
moveDirection.z= inputY * speed * inputModifyFactor;
moveDirection = myTransform.TransformDirection(moveDirection);
}
}
// Apply gravity
moveDirection.y-= gravity * Time.deltaTime;
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;
}
grounded =(controller.Move(moveDirection * Time.deltaTime)& CollisionFlags.Below)!=0;
}
void Update (){
// If the run button is set to toggle, then switch between walk/run speed. (We use Update for this..
// FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
if(toggleRun && grounded && Input.GetButtonDown('Run'))
speed =(speed walkSpeed? runSpeed : walkSpeed);
}
// If the run button is set to toggle, then switch between walk/run speed. (We use Update for this..
// FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event)
if(toggleRun && grounded && Input.GetButtonDown('Run'))
speed =(speed walkSpeed? runSpeed : walkSpeed);
}
// Store point that we're in contact with for use in FixedUpdate if needed
void OnControllerColliderHit (ControllerColliderHit hit){
contactPoint = hit.point;
}
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!');
}
}
// 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!');
}
}
Read Data From an Excel File (.xlsx) in ASP.NET In this article, we will see how to display data from an Excel spreadsheet using ASP.NET. We will connect to a Microsoft Excel workbook using the OLEDB.NET data provider, extract data and then display the data in a GridView. In this article I will explain how to read and display data from a sheet of an Excel file (.xls and.xlsx) and display it in ASP.Net using C# and VB.Net. User browses and selects an Excel Workbook. User selects whether Header row is present in Excel Sheet or not using radio buttons. User uploads the Excel Workbook. Read a file to a DataTable / CUstom Entities (I don't know how to make dynamic properties/fields to an object[column names will be variating in an Excel file]) Use DataTable/Custom Entities to perform some operations using its data. Update DataTable with the results of the operations. Write it back to excel file. Which would be simpler. Read Data From Excel File (xls, xlsx, csv) In ASP.NET MVC In this article, we will see how to display data from an Excel spreadsheet (xlx, xlsx, csv) using ASP.NET MVC. Rajeev Punhani.
Posted by5 years ago
Archived
I was just wondering.. does everyone use the Unity built in character controller? Or do they roll their own?
When you roll your own is it based on Unity's Character controller, does it use the physics system (rigid body?) - or are you using swept boxes?
I'm curious because in our game it's very possible to exploit your way out of the world using the default character controller by forcing yourself into corners and stuff. so I am assuming that most people can't be doing it that way or all Unity games would be terribly buggy.
I've started coding my own swept volume based controller which has worked out pretty well, but I had to code some of my own intersection functions (which I would have thought would have been common). so I'm assuming most people don't do that.
17 comments