Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cooper37 · Sep 11, 2013 at 07:48 PM · movementcontrollervelocityjumping

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);
 
 

 
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cooper37 · Sep 12, 2013 at 04:44 PM 0
Share

Anything? I will post a shorter code later on today.

avatar image getyour411 · Sep 12, 2013 at 07:31 PM 1
Share

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...

avatar image Cooper37 · Sep 17, 2013 at 06:23 PM 0
Share

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.

avatar image Cooper37 · Sep 25, 2013 at 06:57 PM 0
Share

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;
 }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges