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.