How to edit FPSWalker.js?

It is the built-in code of FPSWalker.

I would like to change it to be:

  1. Disable move horizontally
  2. Disable jump
  3. Enable move vertically for certain height or hitting top mesh collider
  4. Enable 360 rotate
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function FixedUpdate() {
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
	}

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)

Here’s your code, It’s modified so that you can go up using “d” and down using “s”, and gravity is disabled, didn’t know if you wanted it or not, and horizontal movement is disabled. this script is untested.

 var jumpSpeed = 8.0; 
var speed = 8.0;

private var moveDirection = Vector3.zero; 

function FixedUpdate() { 
      moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); 
      moveDirection = transform.TransformDirection(moveDirection); 
      moveDirection *= speed; 
       
      if (Input.GetKeyDown("d")) { 
         moveDirection.y = jumpSpeed; 
      } 
      else if (Input.GetKeyDown("s")){
         moveDirection.y = jumpSpeed * -1;
}
   // Move the controller 
   var controller : CharacterController = GetComponent(CharacterController); 
}

@script RequireComponent(CharacterController)

I removed FPSWalker.js and add your script with the name FPSVerticalWalker.js

I run the game but it has no response when I press d and s

Do I need to keep FPSWalker.js as it pop up to say it will lose connection to Prefabs?

Also, I cannot use /* */ or to comment out paragraph of code.

sorry, I did drag the new script to First Person Controller

Try changing the Input.GetKeyDown to just Input.GetKey. If that doesn’t work I’ll look into it.

EDIT: If that doesn’t work, then try this code.

var speed : float = 20.0;
function Update(){
if(Input.GetKey("w")){
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
if(Input.GetKey("s")){
transform.Translate(-Vector3.up * speed * Time.deltaTime);
}
}

The new code works now, it can move vertically only.

There is a plane mesh collider in the bottom, however, First Person Controller can drop beyond it, i.e. -y

Also, how should I set the top move limit of it?

Here, just play around with the upper and lower limit variables to achieve the wanted affect.

var speed : float = 20.0;
var upperLimits : float;
var lowerLimits : float;
function Update(){
if(transform.position.y > upperLimits){
if(Input.GetKey("s")){
transform.Translate(-Vector3.up * speed * Time.deltaTime);
}
}
if(transform.position.y < lowerLimits){
if(Input.GetKey("w")){
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
}
}

The downside is nearly ok, it will drop to -0.02 but not 0, so it has drop beyond the bottom a little bit.

However, it will not move upside.

Are you sure you have the move up high enough? What you should do to find the right numbers to input is look at the Y position your object is currently at then base the number off that.

This code works but I just knew it needs to set upper and lower parameters in inspector.

Originally, I set it in code, so it did not work

var upperLimits : float = 20;
var lowerLimits : float = 0;

Thanks for your great help.

One more question. If it deploys in iPhone, will the ‘s’ and ‘w’ still work?

var speed : float = 20.0; 
var upperLimits : float; 
var lowerLimits : float; 

function Update()
{ 
	if(transform.position.y > lowerLimits)
	{ 
		if(Input.GetKey("s"))
		{ 
			transform.Translate(-Vector3.up * speed * Time.deltaTime); 
		} 
	} 

	if(transform.position.y < upperLimits)
	{ 
		if(Input.GetKey("w"))
		{ 
			transform.Translate(Vector3.up * speed * Time.deltaTime); 
		} 
	} 
}

@script RequireComponent(CharacterController)

So far as the iPhone question, I mean would the multi-touch work with ‘w’ and ‘s’ for move up and downwards? and the self rotate of camera would still work on iphone multi touch screen?

It would pretty much work but you would have to replace the “w” and “s” inputs with either GUI buttons or guitextures that respond like buttons when you touch them. I know how to do both so if you need any further help feel free to PM me.

Dear Sir

I still cannot apply your script to unity iphone.

I found that there is a Player under FPC. Under FPC player, there are 3 scripts:
FirstPersonControl, Left and RightTouchPad

In Unity Basic, there are 2 scripts:
MouseLook.cs

and your script

var speed : float = 20.0; 
var upperLimits : float; 
var lowerLimits : float; 

function Update()
{ 
	if(transform.position.y > lowerLimits)
	{ 
		//if(Input.GetKey("s"))
		if(iPhoneInput.GetTouch)
		{ 
			transform.Translate(-Vector3.up * speed * Time.deltaTime); 
		} 
	} 

	if(transform.position.y < upperLimits)
	{ 
		//if(Input.GetKey("w"))
		if(iPhoneInput.GetTouch))
		{ 
			transform.Translate(Vector3.up * speed * Time.deltaTime); 
		} 
	} 
}

@script RequireComponent(CharacterController)