Character controller / FPS

Hello All,

I’ve been playing with my Unity again and somehow can’t understand what’s going on.

I tried changing the FPS script so that the keyboard input axis(Horizontal) will rotate instead of moving sideways.

Using the simple example from the doc’s I thought I got it working the way I wanted it, but I’m running into a problem that I can’t figure out. I even tried wearing my unify dev hoodie hoping it will help me figure this one out but had no luck.

Can someone “virtually” hit me up the side of my head and help me figure this out.

Using the FPS script;

  1. The player can still move forward or sideways while pressing the jump button.(can hop like a wabbit or a kermit)

  2. The player will collide with any collider and the collider(a cube or a sphere) will not move(can not be pushed).

This is perfect except that I don’t want to use the mouse for my rotation.

Using the keyboard script;

  1. The player can not move forward or rotate once the jump button is press. It will only move again when the jump button is not press( can not hop forward).

  2. The player will collide with any collider but it’s pushing any collider that it collides with.

I’m attaching the FPS script and the KeyboardRotation script.

//The FPS Script(standard)

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;
}

function Awake ()
{
	rigidbody.freezeRotation = true;
	var controller : CharacterController = GetComponent(CharacterController);
	if (!controller)
		gameObject.AddComponent("CharacterController");
}
//Keyboard script with rotation on horizontal
/// This script moves the character controller forward 
/// and rotate left or right based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

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

 
 // added for keyboard rotation(horizontal)
 var rotationspeed = 100.0;
 
 function Awake()
 {
 	rigidbody.freezeRotation = true;
 }
 
function FixedUpdate() 
{
    if (grounded) 
    {
        // We are grounded, so recalculate
        // move direction directly from axes
        var translation = Input.GetAxis("Vertical") * speed;
        var rotation = Input.GetAxis("Horizontal") * rotationspeed;
        
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
        transform.Translate(0,0,translation);
        transform.Rotate(0,rotation,0);
                
        if (Input.GetButton ("Jump")) 
        {
            moveDirection.y = jumpSpeed;
        }    
            //not moving when airborne, this is what I've been trying to do to make it hop

            /*else if(!grounded)
            	{
            		//var translationair = Input.GetAxis("Vertical") * speed;
        			//var rotationair = Input.GetAxis("Horizontal") * rotationspeed;
        
        			//translation *= Time.deltaTime;
       				//rotation *= Time.deltaTime;
            		//transform.Translate(0,0,translation);
            		//transform.Rotate(0,rotation,0);
            		//moveDirection = Vector3(translation,0,rotation);
            		//moveDirection = transform.TransformDirection(moveDirection);
            		//moveDirection *=speed;
            		rigidbody.AddForce (Vector3.forward*translation);
            		rigidbody.velocity.z = translation;
               	}*/
        
    }

    // 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;
}

Any help is appreciated.
Thanks,
Ray

Hi yellowlabrador

You should use the standard FPS_Walker script and change this line of code:

// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”)); [/code]

to:

// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis(“Sidewards”), 0, Input.GetAxis(“Vertical”));[/code]

where Sidewards is a new Key setting in the Input Tag so that you can still walk sidewards.

Then take the standard MouseLook script and change there the part that the Input Tag of the mouse is used to an own defined Tag for your Keys.

That should work than.

By

Okay will be trying it now, just got home

Thanks
Ray

Hey Mike,

I’ve been trying your suggestion but somehow getting stuck with the mouse look side.

I think my brains is stuck in the transform.Translate/Rotate world.
This is what I got right now. FPS is able to hop forward and even when in rotate mode.

My issue is it is not actually rotating it’s actually going in circle(rotating on an origin that it stops.).
Also when I hit a collider, it can still push that collider.

I’m trying to understand about the quaternion rotation on the docs. and also something about clamping. Not sure if that’s the correct approach/solution. At least I think I’m making some progress.

Here’s the script.

/*FPS Controller script that uses the keyboard to move forward(up arrow keys), backward/reverse(down arrow keys) and rotate(left/right arrow keys)360 degrees.
*/

 // The script is not rotationg 360 degrees but going around in circle.
 // Any colliders hit by the FPS still moves.
 
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;

private var grounded : boolean = false;

private var rotationSpeed = 100.0;

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;
		var translation = Input.GetAxis("Vertical")*speed;
		var rotation = Input.GetAxis("Horizontal") * rotationSpeed;
		translation *= Time.deltaTime;
		rotation *= Time.deltaTime;
		transform.Translate(0,0,translation);
		transform.Rotate(0,rotation,0);		
		
				
		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;
}

function Awake ()
{
	rigidbody.freezeRotation = true;
	var controller : CharacterController = GetComponent(CharacterController);
	if (!controller)
		gameObject.AddComponent("CharacterController");
}

Thanks for any input / suggestion regarding the script above.
Ray

Hi yellowlabrador

moveDirection = new Vector3 (Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")); // this is the movement. 
      moveDirection = transform.TransformDirection(moveDirection);
      moveDirection *= speed; 
      var translation = Input.GetAxis("Vertical")*speed; // this isn't needed. put this out.
      var rotation = Input.GetAxis("Horizontal") * rotationSpeed; // you should use here or above another Inputflag like 'horizontalkeys' for rotation and 'horizontal' for walking sidewards.
      translation *= Time.deltaTime; 
      rotation *= Time.deltaTime; 
      transform.Translate(0,0,translation); 
      transform.Rotate(0,rotation,0);

when you make this you should get it worked. When you use the same Inputflag for two tasks you can’t control them seperatly.

I hope this will help you.

Don’t give up hope by trying it (I was trying it 10 years of my life :slight_smile: )

By

L-X-L-ENTE!!

I was forcing to use the arrows keys instead of understanding the input manager.

Now it’s moving the way I wanted it.! Thanks for your help.

I’ve been trying ON and OFF since I first played pacman and donkey kong on “attari 400”.

At least in Unity I can actually do something worthile. :smile:

Thanks again,
Ray