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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by dada5714 · Oct 02, 2012 at 02:23 AM · gamejumpdashfighting

Making an effective Airdash

I've just recently started using Unity3D and it's truly, for lack of better words, awesome. It's almost like a toy box with an unlimited amount of toys.

Anyway, I've been through a few tutorials on 2D platforming controls, and I've applied what I know so far on an innocent cube. But I wanted to add something else to it: an Airdash. I have somewhat of an idea on how to implement it, but my idea currently results in the cube hovering in the air for as long as the WaitForSeconds last.

What I basically want to do is have it to where JS sees that I am FIRST holding either left or right and when I press a dash button (X), I go in the held direction for a certain amount of time without being affected by gravity or letting go of the button. Imagine a Megaman X2 or Marvel vs Capcom airdash.

Excuse the code for being messy. I plan on fixing it once I have the basics implimented.

Any ideas or corrections would be most helpful. Thank you.

 @System.NonSerialized
 private var jumpCount : int = 0;
 
 @System.NonSerialized
 private var gravity : float = 35.0;
 
 @System.NonSerialized
 private var gravenabled : boolean = true;
 
 @System.NonSerialized
 private var inAir : boolean = false;
 
 @System.NonSerialized
 private var velocity : Vector3;
 
 @System.NonSerialized
 private var walkSpeed : float = 7.0;
 
 @System.NonSerialized
 private var runSpeed : float = 15.0;
 
 @System.NonSerialized
 private var jumpSpeed : float = 7.0;
 
 @System.NonSerialized
 private var airdashcount : int = 0;
 
 @System.NonSerialized
 private var airdashspeed : float = 40.0;
 
 class characjump {
 
 @System.NonSerialized
 var height: float = 30.0;
 
 @System.NonSerialized
 var doubleheight: float = 15.0;
 
 @System.NonSerialized
 var verticalSpeed: float = 0.0;
 }
 
 var charjump : characjump;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 var controller : CharacterController;
 controller = GetComponent(CharacterController);
 
 function Move (){
 var h = Input.GetAxisRaw("Horizontal");
 var v = Input.GetAxisRaw("Vertical");
 var airdash: boolean = false;
 var doubletap: float = 0.5;
 var lastTime: float = 0.0;
 var running: boolean = false;
 var moving: boolean = false;
 var canControl: boolean = true;
 
 var isMoving = Mathf.Abs(h) == 1;
 
 
 
 if (controller.isGrounded)
 {
 if (Input.GetKey(KeyCode.DownArrow)||Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow))
 {
 moveDirection.x = 0;
 }
 else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
 {
 moveDirection.x = h;
 moveDirection = transform.TransformDirection(moveDirection);
 moveDirection.x *= (running ? runSpeed : walkSpeed);
 }
 }
 
 else
 {
 if (Input.GetKey(KeyCode.X) && !controller.isGrounded && isMoving)
 {
 airdash = true;
 }
 
 if (airdash && airdashcount == 0){
 
 gravenabled = false;
 moveDirection.x = h;
 moveDirection.y = 0;
 moveDirection = transform.TransformDirection(moveDirection);
 moveDirection.x *= airdashspeed;
 
 yield WaitForSeconds(.3);
 
 gravenabled = true;
 airdashcount = 1;
 }
 
 else
 {
 moveDirection.x = h;
 moveDirection = transform.TransformDirection(moveDirection);
 moveDirection.x *= jumpSpeed;
 }
 }
 }
 
 function Jump ()
 {
 var jump = Input.GetKeyDown(KeyCode.Z);
 var fastfall = Input.GetKeyDown(KeyCode.DownArrow);
 if(!controller.isGround)
 {
 inAir = true;
 }
 
 if(jump)
 {
 
 jumpCount +=1;
 
 
 if (jumpCount == 0)
 {
 moveDirection.y = charjump.height;
 }
 else if (jumpCount == 1)
 {
 gravenabled = true;
 moveDirection.y = charjump.doubleheight;
 gravity = 35.0;
 }
 }
 
 if (inAir && controller.velocity.y <= 0.0){
     if (fastfall)
     {
     gravity = 120.0;
     audio.Play();
     }
 
 }
 
 }
 
 function Gravity (){
 if (gravenabled)
 {
 moveDirection.y -= gravity * Time.deltaTime;
 }
 }
 
 function Update (){
 
 
 Jump();
 Move();
 
 if (controller.isGrounded)
 {
 jumpCount = 0;
 gravity = 35.0;
 airdash = false;
 gravenabled = true;
 airdashcount = 0;
 }
 
 Gravity();
 controller.Move(moveDirection * Time.deltaTime);
 
 }
 
 
 @script RequireComponent (CharacterController)
 @script RequireComponent(AudioSource)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Fattie · Oct 02, 2012 at 07:48 AM

NOTE --

as a rule, if a variable is private, it is pointless to mark it as System.NonSerialized. in your first ten lines you can delete those where you have private.

Within an explicit class definition, I think it's always pointless to use System.NonSerialized, because those are never shown in the inspector anyway.

Gravity - one way to "turn off" an object being affected by gravity temporarily, is, use the very handy ConstantForce component. (Look in menus, component, physics.)

"What I basically want to do is have it to where JS sees that I am FIRST holding either left or right and when I press a dash button (X)"...

do you specifically want the key AA to be pressed BEFORE the key BB?

 Input.GetKey 
 Returns true WHILE the user holds down the key...
 Input.GetKeyDown 
 Returns true DIRING THE FRAME the user starts pressing DOWN the key...



I guess, you could just use those like this:

 if GetKey(AA)  // ok so AA must be down
   if  GetKeyDown(BB) // AND this must be FIRST frame of BB down
     if ! GetKeyDown(AA) // AND this must NOT be FIRST frame of AA down!

I suppose that should work! Normally you'd have booleans keeping track of what the hell is going on, but there's no reason that should work to let you press forwards.

Comment
Add comment · Show 2 · Share
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 dada5714 · Oct 02, 2012 at 08:04 AM 0
Share

About the private issue, for some bizarre reason, it still appeared in my Inspector. So that redundancy was definitely known and fixed.

And ConstantForce seems to definitely be what I was thinking about, but I couldn't figure out the right term. And after looking through the Script Reference, this is definitely it.

And as far as the Input issue, once again, this looks like the answer to my question. I'll edit the script later and see what happens. I'll hold off any questions because I'm sure the answer's staring me in the face.

Thanks once again!

avatar image dada5714 · Oct 02, 2012 at 08:18 AM 0
Share

One more quick question. Will constantForce still work on this if it's a character controller?

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

10 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

Related Questions

My script only partially works 2 Answers

How could i execute a command move for a fighting game? 3 Answers

Problem with jumping 1 Answer

Help Me with My Script Pls?????? 1 Answer

Delay for jumping, I've looked everywhere. 2 Answers


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