Constant Force applied to player without rigidbody on button press

Okay, I want to make myself very clear. My goal is to have a player be able to press and hold jump after holding rightclick to boost in the opposite direction of gravity. Im talking about a constant force, not just a jump. If the player releases jump the force stops. If while in mid air the player holds jump down again, the boost is re-activated. After reading about jetpacks and whatnot around Unity, I found that a lot of people use rigidbodies. I gave that a go, but my player just randomly rotates (including upside down). So I made the rigid body Kinematic. That fixed the rotation problem, but then every example of code I tried for the boost said: Actor::setLinearVelocity: Actor must be (non-kinematic) dynamic!
UnityEngine.Rigidbody:set_velocity(Vector3)

So here is what I am using now.
The code I started out with is from here: Unity - Scripting API: CharacterController.Move

Here is my edited code.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var boost : float = 30;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {

	var controller : CharacterController = GetComponent(CharacterController);
	if (controller.isGrounded) {
		// We are grounded, so recalculate
		// move direction directly from axes
		moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
		                        Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
		
		if (Input.GetButton ("Fire2")) {
			
			//Add friction mod here
			
			//This is the upward force
			if (Input.GetButton ("Jump")) {
			//I have tried many things here to no avail
		}
		}
		
	}
	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	controller.Move(moveDirection * Time.deltaTime);
}

I plan on making it to where I can rightclick and have zero friction, but that will come later as well as a fuel supply.

This should work kinda the way you wanted it. I know you don’t want to use a rigidbody, but if you want more realistic effects it would be a lot easier to use the Unity physics engine instead of trying to build your own little physics engine :slight_smile:

As you can see below, you have to handle the situation when you are not grounded. The original example code just continues to move in the same direction during the jump - while applying gravity to fall down - until the player hits the ground again.

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var boostSpeed : float = 6.0;
var gravity : float = 20.0;
private var boostAllowed : boolean = false;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
	var controller : CharacterController = GetComponent(CharacterController);
	if (controller.isGrounded) {
		// We are grounded, so recalculate
		// move direction directly from axes
		moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
		                        Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			if (Input.GetMouseButton(1)) {
				// use the jetpack without jumping
				boostAllowed = true;
				moveDirection.y = boostSpeed;
			} else {
				// just a normal jump
				boostAllowed = false;
				moveDirection.y = jumpSpeed;
			}
		}
	} else {
		// we are not grounded
		// use boost if allowed
		if (boostAllowed && Input.GetButton("Jump")) {
			// If you want to change direction while boosting
			// uncomment the following block
			/*
			moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
			                        Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= speed;
			*/
			moveDirection.y = boostSpeed;
		}
	}
	
	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	controller.Move(moveDirection * Time.deltaTime);
}