- Home /
In-Air Movement for Custom Controller?
I have created a custom controller for a 3D platformer and it's pretty much exactly what I need. The only thing is, once I'm in the air, the object cannot be controlled anymore until it hits the ground. I was wondering if someone could help me in adding some in-air movement to my controller. Also, if you can, when I jump while staying still, the player snaps to the front(z-axis). If you can help me out with that, I would be grateful.
I know it's a lot, but I don't think it's too hard to grasp. If you need a shorter script, let me know.
/*PLAYER SETUP:
For moving platforms, make a gameobject with a scale of (1,1,1), place the platform "model" and the
trigger with tag "Platform"(children) fitting the model INSIDE that gameobject, then animate/ move /
position the gameobject(parent).
For jump pads, add a sphere collider set as Trigger and tagged with "Jump Pad" on the graphics/model,
and add a child with a collider fitting the model.
*/
#pragma strict
#pragma implicit
#pragma downcast
var moveSpeed : float = 12.0;
var rotateSpeed : float = 200.0;
var personalSpace : float = 4.0;
var jumpHeight : float = 15.0;
var gravity : float = 30.0;
var swingTime : float = 0.5;//The less of the value, the faster the swing
var doubleHeight : float = 4.0;
var padHeight : float = 25.0;
//--------------------------------------------------------------------------------
private var rate : float = 0.7;
private var canMove = true;
private var canJump = true;
private var canDoubleJump = true;
private var moveDirection = Vector3.zero;
private var controller : CharacterController;
private var rotation : Vector3;
private var cooldown = false;
var walkSound : AudioClip;
var runSound : AudioClip;
var jumpSound : AudioClip;
var flipSound : AudioClip;
var landSound : AudioClip;
var SwingISound : AudioClip;
var SwingIISound : AudioClip;
var IdleAni : AnimationClip;
var WalkAni : AnimationClip;
var RunAni : AnimationClip;
var JumpAni : AnimationClip;
var DoubleJumpAni : AnimationClip;
var AttackAni : AnimationClip;
var AttackComboAni : AnimationClip;
var ThirdAttackAni : AnimationClip;
//"Idle"
//"Walk"
//"Run"
//"Jump"
//"DoubleJump"
//"Attack"
//"AttackCombo"
//"ThirdAttack"
function Awake(){
controller = GetComponent(CharacterController);
}
function Update(){
//--------------------MOVEMENT & JUMPING--------------------------------------------------
//----------------------------------------------------------------------------------------
if(controller.velocity.sqrMagnitude < 0.1){
//animation.CrossFade("IdleAni");
}
if(controller.isGrounded && canMove)
{
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;
//animation.CrossFade("WalkAni");
canDoubleJump = true;
if(Input.GetButton("Jump") && canJump)
{
Jump();
}
}
if(!controller.isGrounded)
{
if(canDoubleJump && canJump && Input.GetButtonDown("Jump")){
DoubleJump();
}
}
if(moveDirection != Vector3.zero)
{//Looks at its direction if it's not facing Z forward
var rotation = transform.rotation;
rotation.SetLookRotation(new Vector3(moveDirection.x,0,moveDirection.z) * rotateSpeed);
transform.rotation = rotation;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftShift) && controller.isGrounded && canMove){
Run();
}
//-------------------------EXECUTE ATTACKS----------------------------------------------------
//--------------------------------------------------------------------------------------------
if(Input.GetMouseButtonDown(0) && controller.isGrounded && !cooldown){
AttackI();
}
if(Input.GetMouseButtonDown(1) && controller.isGrounded && cooldown){
AttackII();
}
}
//--------------------------ACTIONS----------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
function Run(){
//animation.CrossFade("RunAni");
controller.Move(moveDirection * Time.deltaTime);
}
function AttackI(){
Debug.Log("Attack I");
cooldown = true;
//animation.CrossFade("AttackAni");
yield WaitForSeconds(rate);
cooldown = false;
}
function AttackII(){
Debug.Log("Attack II");
//animation.CrossFade("AttackComboAni");
cooldown = false;
}
function SpellAttack(){
}
function Jump(){
moveDirection.y = jumpHeight;
//animation.CrossFade("JumpAni");
}
function DoubleJump(){
moveDirection.y = jumpHeight + doubleHeight;
canDoubleJump = false;
//animation.CrossFade("DoubleJumpAni");
}
//---------------------------WORLD INTERACTIONS-----------------------------------------------------
//--------------------------------------------------------------------------------------------------
function OnTriggerStay(other : Collider){
//MOVING ON MOVING PLATFORMS
if(other.tag == "Platform")
{
this.transform.parent = other.transform.parent;
}
}
function OnTriggerEnter(other : Collider){
//JUMPING ONTO JUMP PADS
if(other.tag == "Jump Pad")
{
moveDirection.y = padHeight;
//Jump
canDoubleJump = true;
controller.enabled = true;
other.animation.Play("Jump Pad");
}
}
function OnTriggerExit(other : Collider){
//MOVING OFF MOVING PLATFORMS
if(other.tag == "Platform")
{
this.transform.parent = null;
}
}
//-------------------------------TOOLS--------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
function OnDrawGizmosSelected(){
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, personalSpace);
}
@script RequireComponent(CharacterController);
66
if(controller.isGrounded && can$$anonymous$$ove)
You probably want something like
if((controller.isGrounded && can$$anonymous$$ove) || isAirControlled)
100
moveDirection.y -= gravity * Time.deltaTime;
You'll want to control that
if(!isAirControlled)
moveDirection.y -= gravity * Time.deltaTime
Some ideas to get you going...
I don't think it's the y that needs to be played with, more like moveDirection.x moveDirection.z
But I don't know where to go from there.
Forgot the shorter code: var moveSpeed : float = 18.0; var jumpHeight : float = 15.0; var gravity : float = 30.0; var doubleHeight : float = 4.0;
private var moveDirection : Vector3 = Vector3.zero;
private var controller : CharacterController;
private var canJump = true;
private var canDoubleJump = true;
private var rotation : Vector3;
function Update() {
if(controller.velocity.sqr$$anonymous$$agnitude < 0.1){
//animation.CrossFade("IdleAni");
}
if(controller.isGrounded){
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");
var input$$anonymous$$odifyFactor = (h != 0.0 && v != 0.0)? .7071 : 1.0;
moveDirection = (h * right + v * forward);
moveDirection *= moveSpeed;
canDoubleJump = true;
if(Input.GetButton("Jump") && canJump){ //JU$$anonymous$$PING INPUT
Jump();
}
}
if(!controller.isGrounded){
if(canDoubleJump && canJump && Input.GetButtonDown("Jump")){
DoubleJump();
}
}
if(moveDirection != Vector3.zero){
var rotation0 = transform.rotation;
rotation0.SetLookRotation(new Vector3(moveDirection.x,0,moveDirection.z) * rotateSpeed);
transform.localRotation = rotation0;
}
//animation.CrossFade("RunAni");
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
function Jump(){
moveDirection.y = jumpHeight;
}
function DoubleJump(){
moveDirection.y = jumpHeight + doubleHeight;
canDoubleJump = false;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613114000im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Return Proper Velocity of Player object 3 Answers
CharacterController Jump from moving platform. 0 Answers
Moving a Simple Ball [Main Player!] inside a tunnel without gravity! 1 Answer
How to make an object moving in a certain direction, move in specific steps like 0.1f 1 Answer