FPLazyControll (renamed: was FPJumper)

I am trying to create a script with different type of movement than the FPSWalker script.

Control + LMouseClick - Jump to Mouse position
Shift + LeftMoseClick - Rotate to Mouse position
LeftMouseLick - Walk to Mouse position

I have managed to make the Player Jump using this code

if (Input.GetMouseButtonDown (0)) {
	moveDirection.y = jumpSpeed;
}

But I cant get it to recoginise the control Key:

Input.GetButton (KeyCode.LeftControl)

Causes an error? - How do I reference the control key? And how do i get it to recognise where on the plane the user has clicked? From what I can tell, I will be using the mouse X and Z co-ords?

I’ll Keep playing with it, but any help is appreciated.
Thanks.
Ricardo.

Okay, I have found that I was setting jump in the wrong place.
I changed it in the InputManager config to the mouse button and the left ctrl button. Now i just have to figure out how to make it use both, instead of either or?

I guess thats the part that goes into the script

Ok, I have some improvements, but I still dont fully understand what im doing, i guess.

Can anyone tell me if im on the right track?

My intention is for the following:

Control + LeftMouseClick - Jump and Rotate to Mouse position
Shift + LeftMouseClick - Rotate to Mouse position on the spot
LeftMouseClick - Rotate and Walk to Mouse position

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

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


function Update() {
	var controller : CharacterController = GetComponent(CharacterController);
	var hit : RaycastHit;
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	
	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")) {
			if (Input.GetMouseButtonDown (0)) {
				//Jump to position
				if (Physics.Raycast (ray, hit, 100)) {
    				          transform.position = Vector3.Slerp( hit.point, ray.origin, 0.1);
    				}
			}
		} else if (Input.GetButton ("Turn")) {
		     if (Input.GetMouseButtonDown (0)) { 
		           //rotate the player
		           transform.Rotate(0,100,0);
		      }
		} else if (Input.GetMouseButtonDown (0)) { 
			// we are walking, just move to the point	
		}
	}

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


@script RequireComponent(CharacterController)

I guess I should also mention that the camera is a fixed height from the plane, and does not rotate with the player.

Thanks.

Still not quite there yet. I have the control+leftMouseClick to jump to the position.
And the walk, goes to the position, but there is no animation between. The controller just appears on the clicked position.

var speed = 50;
var jumpSpeed = 50;
var gravity = 100;
var target : Transform;

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


function FixedUpdate() {
	var controller : CharacterController = GetComponent(CharacterController);
	var ray = camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit; 
    
    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")) {
			if (Input.GetMouseButtonDown (0)) {
				if (Physics.Raycast (ray, hit, 10000)) {
    				      moveDirection.z = hit.point.z - target.position.z;
    				      moveDirection.x = hit.point.x - target.position.x;
    				      moveDirection.y = jumpSpeed;	
    			        }	
			}	
		} else if (Input.GetButton ("Turn")) {
			if (Input.GetMouseButtonDown (0)) { 
		        //RotatePlayer();
		   }
		} else if (Input.GetMouseButtonDown (0)) { 
			//walk to position
			if (Physics.Raycast (ray, hit, 10000)) {
   				moveDirection.z = hit.point.z - target.position.z;
   				moveDirection.x = hit.point.x - target.position.x;
   				moveDirection *= speed;
   			}	
		}
	}

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

The script is applied to the FPController and the same controller is set as the target, as it was the only way i could figure out how to get the controllers current position.

How can I make the controller animate to the clicked position instead of just appearing there when walking, like it does when jumping?

Thanks.

I have got it the walking part working. I just need to fix a few issues with it. I will add the rotate, but I will leave the rest until later.

The main issues are with speed and jump height. As they are both fixed. And I need to get them relative to the hit.point. But anyway, thought I should post some updated code in the hopes that it will help someone, some day :slight_smile:

The Issue I was having before with it appearing on the hit.point instead of animating (walking) there, was because the function was being called again (on the FiexedUpdate) before it got to its destination, so the system would skip the animation. I bypassed this by flagging if the controller was currently moving or not. Simple enough I guess. But as the noob that I am, it took me 2 days of trying, to get it to work.

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var target : Transform;

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

private var moving = false;
private var moveTo = Vector3(0,0,0);
	
private var originalRotation;

function FixedUpdate() {
	var controller : CharacterController = GetComponent(CharacterController);
	var ray = camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit; 
    
    print(target.position + " " + moveTo);
    
    if ((target.position.z == moveTo.z)  (target.position.x == moveTo.x)) {
    	moving = false;	
    }
    
    if (grounded  !moving) {
		// 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")) {
			if (Input.GetMouseButtonDown (0)) {
				if (Physics.Raycast (ray, hit, 10000)) {
    				moveDirection.z = hit.point.z - target.position.z;
    				moveDirection.x = hit.point.x - target.position.x;
    				moveDirection.y = jumpSpeed;
    				moveTo = hit.point;	
    			}	
				
			}	
		} else if (Input.GetButton ("Turn")) {
			if (Input.GetMouseButtonDown (0)) { 
		      //transform.Rotate(0,100,0);
		      //moveDirection.y = distanceToGround;
				RotatePlayer();
		   }
		} else if (Input.GetMouseButtonDown (0)) { 
			if (Physics.Raycast (ray, hit, 10000)) {
				moving = true;
   				moveDirection.z = hit.point.z - target.position.z;
   				moveDirection.x = hit.point.x - target.position.x;
   				moveTo = hit.point;
   				moveDirection *= speed;
   			}	
		}
	}

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