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();
}
}
}
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.
How to add 2 touches? Can you help me ,please?
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
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.
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
Follow this Question
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