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 ProgrammerInTraining · Jun 06, 2014 at 03:07 AM · androidjavascript3djumping

How To Create Android 3d Game Jump Button

Since the site had a hiccup and erased everything I just entered except the tags and title after freezing up, I am going to make this one short and sweet:

I have been trying to learn 3D Game Development with Unity3D Android. To do this, I have spent a week going through youtube video tutorials by Pierce Thompson. This guy cut his first set of videos off then his 2nd set was even more all over the place. He somehow fixed his code to make his character jump by first hitting the "Stop Recording" button on his camera then the "Resume Recording" button...then didn't post his finished code or changes. I'm no master coder but that doesn't seem like it would work.

I have fixed a bunch of his broken code and added a few extras in on my own by trial and error. However, I just can't figure out how to do two things:

1) Create a button instead of a joystick

2) Make the character jump

The closest I have gotten is using the tapCount of a joystick to increase the lateral movement speed of the character's current movement direction by adding jumpSpeed. I have not successfully gotten the character off the ground....unless I run off the side of the terrain...at which time there is no more ground so I just keep falling LOL.

I like to figure things out on my own but with this, I have done about 150 google searches, unity community searches, and tutorial reviews and have not found anything on this subject.

Please help me do the following two things:

1) Change my JumpPad from joystick to button

2) Get my character to jump

The following is in JavaScript:

 //Says this script is required to run the game
 @script RequireComponent(CharacterController)
 
 //Default access variables
 var LeftJoy : Joystick;
 var RightJoy : Joystick;
 var JumpPad : Joystick;
 //For rotation of camera
 var cameraPivot : Transform;
 //Movement
 var forwardSpeed : float = 4f;
 var backwardSpeed : float = 2f;
 var sidestepSpeed : float = 2f;
 var jumpSpeed : float = 8f;
 var inAirMultiplier : float = 0.25f;
 var rotationSpeed : Vector2 = Vector2(50, 25);
 var tiltPositiveYAxis = 0.6;
 var tiltNegativeYAxis = 0.4;
 var tiltXAxisMinimum = 0.1;
 var Turtle : GameObject;
 
 //Private variable declarations
 private var thisTransform : Transform;
 private var character : CharacterController;
 private var cameraVelocity : Vector3;
 private var velocity : Vector3;
 private var canJump = true;
 
 //Called at start of game
 function Start ()
 {
 
     //Defines components to speed up in game loading time
     thisTransform = GetComponent(Transform);
     character = GetComponent(CharacterController);
     Turtle = GameObject.FindGameObjectWithTag("Turtle");
     LeftJoy = GameObject.FindGameObjectWithTag("LeftJoy").GetComponent(Joystick);
     RightJoy = GameObject.FindGameObjectWithTag("RightJoy").GetComponent(Joystick);
     JumpPad = GameObject.FindGameObjectWithTag("JumpPad").GetComponent(Joystick);
     //Move player to spawn point if it exists
     var spawn = GameObject.Find("PlayerSpawn");
     if(spawn)
     {
     
         thisTransform.position = spawn.transform.position;
     
     }
 
 }
 
 //Function for when game ends
 function onEndGame()
 {
 
     LeftJoy.Disable();
     RightJoy.Disable();
     JumpPad.Disable();
     this.enabled = false;
 
 }
 
 //Called every frame
 function Update ()
 {
 
     var movement = thisTransform.TransformDirection(Vector3(LeftJoy.position.x, 0, LeftJoy.position.y));
     //Normalizes frame rate like Delta Time.  Equalizes frame rate from code to device allowability
     movement.Normalize();
     var absJoyPos = Vector2(Mathf.Abs(LeftJoy.position.x), Mathf.Abs(LeftJoy.position.y));
     
     if(absJoyPos.y > absJoyPos.x)
     {
     
         if(LeftJoy.position.y > 0)
         {
     
             movement *= forwardSpeed * absJoyPos.y;
             Turtle.animation["Turtle_Walk"].speed = LeftJoy.position.y;
             Turtle.animation.CrossFade("Turtle_Walk");
             
         }
         
         else
         {
         
             movement *= backwardSpeed * absJoyPos.y;
             Turtle.animation["Turtle_Walk"].speed = LeftJoy.position.y;
             Turtle.animation.CrossFade("Turtle_Walk");
         
         }
     
     }
     
     else if(absJoyPos.y == absJoyPos.x)
     {
     
         Turtle.animation["Turtle_Idle"].speed = 0.75f;
         Turtle.animation.CrossFade("Turtle_Idle");
     
     }
     
     else
     {
         
             movement *= sidestepSpeed * absJoyPos.x;
             Turtle.animation["Turtle_Walk"].speed = LeftJoy.position.x;
             Turtle.animation.CrossFade("Turtle_Walk");
     
     }
     
     if(character.isGrounded)
     {
     
         var jump = false;
         
         if(!JumpPad.IsFingerDown())
         {
         
             canJump = true;
         
         }
         
         if(JumpPad.IsFingerDown())
         {
         
             print("jump button pressed");
             
             //Jump syntax goes here
         
         }
     
     }
     
     else
     {
     
         velocity.y += Physics.gravity.y * Time.deltaTime;
         
         movement.x *= inAirMultiplier;
         movement.z *= inAirMultiplier;
     
     }
     
     movement += velocity;
     movement += Physics.gravity;
     movement *= Time.deltaTime;
     
     character.Move(movement);
     
     if(character.isGrounded)
     {
     
         var camRotation = Vector2.zero;
         
         if(RightJoy)
         {
         
             camRotation = RightJoy.position;
         
         }
         
         camRotation.x *= rotationSpeed.x;
         camRotation.y *= rotationSpeed.y;
         camRotation *= Time.deltaTime;
         
         thisTransform.Rotate(0, camRotation.x, 0, Space.World);
         
         cameraPivot.Rotate(-camRotation.y, 0, 0);
     
     }
 
 }
Comment
Add comment · Show 3
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 Jeff-Kesselman · Jun 08, 2014 at 06:34 PM 1
Share

Why is jumping anything more then just adding a force in the jump direction?

avatar image ProgrammerInTraining · Jun 08, 2014 at 07:02 PM 0
Share

Anybody? Seems like this whole 3 axis movement system is a massive secret in the Unity Community. Well, I'll keep working on my end. If I figure it out, I'll be an awesome guy and post my code for everybody to see and use and make more efficient.

Thanks to anybody who still ends up giving any assistance.

avatar image ProgrammerInTraining · Jun 08, 2014 at 10:30 PM 0
Share

$$anonymous$$y thoughts exactly @Jeff $$anonymous$$esselman.

I just can't seem to wrap my head around what this Pierce Thompson youtube guy did with the entire setup. To me it seems like the Vector3 setup for variable "movement" creation should have another argument for JumpPad where the 0 is but that didn't seem to do anything.

I also tried just using movement.y but that didn't seem to work either, then I tried with velocity.y since he has a reference to physics.gravity.y attached to variable velocity.y but that didn't seem to do anything either.

Closest I have come is while moving forward, backward, left, or right, hitting the JumpPad joystick but all that does is make me go even faster in the direction I am walking until I let go of the JumpPad joystick. So I tried taking the setup that made me go faster in walking direction and altering it for movement on the Y-Axis but that was a fail too.

Trust me...I usually don't hop on forums unless I absolutely have to...and with this, I am 110% stuck at this point.

2 Replies

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

Answer by ProgrammerInTraining · Jun 09, 2014 at 12:48 AM

Thanks for your response @screenname_taken.

However, I actually just stumbled upon the answer. I was missing the Vector3.up add in to the jumpSpeed multiplier. The corrected code is below:

     if(character.isGrounded)
     {
         
         if(JumpPad.tapCount >= 1)
         {
         
             //Jump syntax goes here
             velocity += jumpSpeed * Vector3.up;
         
         }
     
     }
     
     else
     {
     
         velocity.y += Physics.gravity.y * Time.deltaTime;
         
         movement.x *= inAirMultiplier;
         movement.z *= inAirMultiplier;
     
     }

Thank you for the assistance. I hope this little entry helps other beginners in the 3D Game development world.

Comment
Add comment · 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
0

Answer by screenname_taken · Jun 08, 2014 at 11:01 PM

To make my ball jump i just added a GUITexture on the screen, with the graphic of a button...

Then added a script to it with the touch code from Unity's documentation.

 for (var touch : Touch in Input.touches) {
     if (touch.phase == TouchPhase.Began&&jumpBTN.guiTexture.HitTest(touch.position)&&onGround){
        jumpReady();
        }
 }

Mind you, the onGround boolean is a test i'm doing elsewhere. The jumpReady() is just a function i'm calling to make the ball jump with AddForce and do some particle effects.

So, the guiTexture i put on the screen is the "jumpBTN" that i'm testing against and you need to assign it in the editor on the script component, or have the script find the guiTexture and assign in itself in Start().

EDIT: i'm using AddForce because the character is just a ball with a rigidbody. It doesn't have a jump animation or anything. You'd replace jumpReady with whatever you need. I'm guessing you just needed the part to check for a touch input.

Comment
Add comment · 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

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Jump Script for 2D (JavaScript) 0 Answers

Java or JavaScript? 1 Answer

3D Game-Making a character jump 3 Answers

BCE0051: Operator '<' cannot be used with a left hand side of type 'Object' and a right hand side of type 'int'. 1 Answer

Unity 2D Mobile Game Drawing Mechanic 0 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