Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by McoGames · May 01, 2020 at 09:28 PM · controllertimejump3rd person controllersame

What can I do to jump when I move with a virtual joystick?

My game is 3-rd person for android with virtual joystick. I have a problem. When I'm moving I can't jump at the same time. This is my script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour {

 public float movementSpeed = 3;
 public float jumpForce = 300;
 public float timeBeforeNextJump = 1.2f;
 private float canJump = 0f;
 Animator anim;
 Rigidbody rb;
 public Joystick joystick;
 
 void Start()
 {
     anim = GetComponent<Animator>();
     rb = GetComponent<Rigidbody>();
 }

 void Update()
 {
     ControllPlayer();
     //
     
     //
 }

 void ControllPlayer()
 {
    

     

     float moveHorizontal = joystick.Horizontal;
     float moveVertical = joystick.Vertical;
     
     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
     
     if (movement != Vector3.zero)
     {
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15f);
         anim.SetInteger("New Int", 1);
     }
     else {
         anim.SetInteger("New Int", 0);
     }

     transform.Translate(movement * movementSpeed * Time.deltaTime, Space.World);

      

     if (Input.GetMouseButtonDown(0) && Time.time > canJump || Input.GetButtonDown("Jump") && Time.time > canJump)
     {
             rb.AddForce(0, jumpForce, 0);
             canJump = Time.time + timeBeforeNextJump;
             anim.SetTrigger("jump");
     }    
 }

 void FixedUpdate()
 {
     if (rb.position.y < -0.5f)
     {
         FindObjectOfType<GameManager>().EndGame();
     }
 }

}

Comment
Add comment · Show 2
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 highpockets · May 01, 2020 at 09:59 PM 0
Share

I dint see how you are handling touch input because there is no touch input code here, but I assume that is where the problem would be. You need to be able to listen and respond to at least 2 touches.

avatar image McoGames highpockets · May 02, 2020 at 11:34 AM 0
Share

How to add 2 touches? Can you help me ,please?

1 Reply

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

Answer by highpockets · May 02, 2020 at 12:23 PM

Ok well to handle touches you can find a lot of info in the documentation:


https://docs.unity3d.com/ScriptReference/Input.GetTouch.html


Input.getTouch(0) will return the first touch and (1) will give you the second touch as Touch structs. I can’t really explain it better than the documentation, but for touch gestures I personally use a free asset provided and maintained by prime31 called TKtouchkit which you can find on GitHub and it comes with a lot of built in gestures, but you can extend it quite easily to make tailored gestures for your specific case

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 McoGames · May 02, 2020 at 03:20 PM 0
Share

I try to use TKtouchkit, but i can't understand how to use it from the discription. When i try to add it to empty object "TouchKit" it gives me many errors in the script? How to add it? I looked at the demo scenes , but in my project gives me many errors.

avatar image McoGames · May 02, 2020 at 06:15 PM 0
Share

I moved my jump comand from character movement script to this and it works! Thank you very much! Have a nice day! I made it with this script :

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ExampleClass : $$anonymous$$onoBehaviour {

 void Start() {
     rb = GetComponent<Rigidbody>();
 }
 public GameObject player;
 public float jumpForce = 300;
 public float timeBeforeNextJump = 0.5f;
 private float canJump = 0f;
 Rigidbody rb; 

 // Update is called once per frame
 void Update()
 {
     for (int i = 0; i < Input.touchCount; ++i)
     {
         if (Input.GetTouch(i).phase == TouchPhase.Began && Time.time > canJump)
         {
             rb.AddForce(0, jumpForce, 0);
             canJump = Time.time + timeBeforeNextJump;
         }
     }
 }

}

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

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

how can i repeat an if statment for x sec? 1 Answer

Make 3D character controller not stuck on walls 0 Answers

Change 3rd person controller jump 0 Answers

Ball Bouncing Game(When I touch on ball) 0 Answers

Basic MOVE and JUMP script (Jump trouble) 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