I almost done with my custom controller, been working on it for a week. UGH! But there’s just one little thing. When the player jumps, I need it to keep facing parallel with the XZ plane. For some reason, the object sort of looks up when I jump, then when falling, it looks down. How would I be able to restrict the object from looking at the y axis? This is the script I have now. I know it’s big, but you may attach it to a cube and have fun with it, and see what I’m talking about. Also, if you could give me some help on how I can do the In Air Controls, that would be great. Thanks!
#pragma strict
#pragma implicit
#pragma downcast
//OUTSIDE SCRIPT VARIABLES
var canMove = true;
var moveSpeed : float = 4.0;
var rotateSpeed : float = 200.0;
var canJump = true;
var jumpHeight : float = 12.0;
var gravity : float = 30.0;
var canDoubleJump = true;
var doubleHeight : float = 4.0;
var padHeight : float = 25.0;
private var moveDirection = Vector3.zero;
function Update(){
var controller : CharacterController = GetComponent(CharacterController);
//ENABLES THE MOVEMENT
if(controller.isGrounded canMove)
{//(X,Y,Z)
// moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
//moveDirection = transform.TransformDirection(moveDirection);
forward = Camera.main.transform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
right = new Vector3(forward.z,0,-forward.x);
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
moveDirection = (h * right + v * forward);
moveDirection *= moveSpeed;
canDoubleJump = true;
//CONTROLS JUMPING
if(Input.GetButton ("Jump") canJump)
{
moveDirection.y = jumpHeight;
}
}
//CONTROLS JUMPING IN AIR
if(!controller.isGrounded)
{
}
//DOUBLE JUMPING
if(!controller.isGrounded canDoubleJump canJump Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpHeight + doubleHeight;
canDoubleJump = false;
}
if(moveDirection != Vector3.zero)
{//Looks at its direction
var rotation = transform.rotation;
rotation.SetLookRotation(moveDirection * rotateSpeed);
transform.rotation = rotation;
}//Applys gravity and movement
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
function OnTriggerStay(other : Collider){
//MOVING ON PLATFORMS//
if(other.tag == "Platform")
{
this.transform.parent = other.transform.parent;
}
/*Make a gameobject with a scale of (1,1,1), place the platform model and the
trigger with tag "Platform"(children) INSIDE that gameobject, then animate/ move /
position the gameobject(parent).
*/
}
function OnTriggerEnter(other : Collider){
//JUMPING ONTO JUMP PADS
if(other.tag == "Jump Pad")
{
moveDirection.y = padHeight;
}
}
function OnTriggerExit(other : Collider){
//MOVING OFF PLATFORMS
if(other.tag == "Platform")
{
this.transform.parent = null;
}
}
@script RequireComponent(CharacterController);