Revision as of 21:47, 24 October 2012

Contents

Description

Someone on the forum was struggling to do a really simple thing: make a rotating bridge that an FPS Walker can stand on and be carried around in a circle.

The FPS Walker / Character Controller that UT provides will not do this so I made a script that works by physics and therefore gets friction information from other objects.

Usage

Assign this script to a rigid body with a collider and it will be able to run, jump and ride on moving platforms etc.

JavaScript - New FPS Controller.js

// These variables are for adjusting in the inspector how the object behaves var maxSpeed =7.000;var force =8.000;var jumpSpeed =5.000;// These variables are there for use by the script and don't need to be editedprivatevar state =0;privatevar grounded =false;privatevar jumpLimit =0;// Don't let the Physics Engine rotate this physics object so it doesn't fall over when runningfunction Awake (){
rigidbody.freezeRotation=true;}// This part detects whether or not the object is grounded and stores it in a variablefunction OnCollisionEnter (){
state ++;if(state >0){
grounded =true;}}function OnCollisionExit (){
state --;if(state <1){
grounded =false;
state =0;}}// This is called every physics framefunction FixedUpdate (){// Get the input and set variables for itvar jump = Input.GetButtonDown("Jump");var horizontal = Input.GetAxis("Horizontal");var vertical = Input.GetAxis("Vertical");// Set the movement input to be the force to apply to the player every frame
horizontal *= force;
vertical *= force;// If the object is grounded and isn't moving at the max speed or higher apply force to move itif(rigidbody.velocity.magnitude< maxSpeed && grounded ==true){
rigidbody.AddForce(transform.rotation* Vector3.forward* vertical);
rigidbody.AddForce(transform.rotation* Vector3.right* horizontal);}// This part is for jumping. I only let jump force be applied every 10 physics frames so// the player can't somehow get a huge velocity due to multiple jumps in a very short timeif(jumpLimit <10) jumpLimit ++;if(jump && grounded ==true&& jumpLimit >=10){
rigidbody.velocity.y+= jumpSpeed;
jumpLimit =0;}}

C Sharp

usingUnityEngine;usingSystem.Collections;publicclass PhysicsFPSWalker : MonoBehaviour {// These variables are for adjusting in the inspector how the object behaves publicfloat maxSpeed =7;publicfloat force =8;publicfloat jumpSpeed =5;// These variables are there for use by the script and don't need to be editedprivateint state =0;privatebool grounded =false;privatefloat jumpLimit =0;// Don't let the Physics Engine rotate this physics object so it doesn't fall over when runningvoid Awake (){
rigidbody.freezeRotation=true;}// This part detects whether or not the object is grounded and stores it in a variablevoid OnCollisionEnter (){
state ++;if(state >0){
grounded =true;}}void OnCollisionExit (){
state --;if(state <1){
grounded =false;
state =0;}}publicvirtualbool jump
{
get
{return Input.GetButtonDown("Jump");}}publicvirtualfloat horizontal
{
get
{return Input.GetAxis("Horizontal")* force;}}publicvirtualfloat vertical
{
get
{return Input.GetAxis("Vertical")* force;}}// This is called every physics framevoid FixedUpdate (){// If the object is grounded and isn't moving at the max speed or higher apply force to move itif(rigidbody.velocity.magnitude< maxSpeed && grounded ==true){
rigidbody.AddForce(transform.rotation* Vector3.forward* vertical);
rigidbody.AddForce(transform.rotation* Vector3.right* horizontal);}// This part is for jumping. I only let jump force be applied every 10 physics frames so// the player can't somehow get a huge velocity due to multiple jumps in a very short timeif(jumpLimit <10) jumpLimit ++;if(jump && grounded && jumpLimit >=10){
rigidbody.velocity= rigidbody.velocity+(Vector3.up* jumpSpeed);
jumpLimit =0;}}}