- Home /
I Watched The Unity New Ball Rolling Game Tut, How I Can Make Jump,I Watched The New Unity Ball Rolling Tutorial, How I Can Make Jump?
I Watched The Whole Unity Ball Rolling Game Tutorial(New) But The Didin't Tell How To Make Jump So How I Can Make Jump With The New Unity Input System Heres The Code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using TMPro;
public class PlayerController : MonoBehaviour { public float speed; public TextMeshProUGUI countText; public GameObject winTextObject;
private Rigidbody rb;
private int count;
private float movementX;
private float movementY;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winTextObject.SetActive(false);
}
void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if(count >= 11)
{
winTextObject.SetActive(true);
}
}
void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
}
you need a jump method that changed the velocity of the rigidbody on the game object to increase by X amount when i dunno
float jumpAmount = 10;
void Update()
{
if(input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
void Jump()
{
//As long as there is a rigidbody on the object and gravity is in effect then:
gameObject.GetComponent<Rigidbody>().velocity = new Vectory3(transform.position.x, transform.position.y + jumpAmount,transform.position.z) ;
}
were pressed in the update.
NOTE: untested just wrote from memory -> might be wrong.
Your answer
Follow this Question
Related Questions
Using a touch event for character motor 0 Answers
Making character to fly 1 Answer
Character Controller sticky head.... Help!? 1 Answer
How do I fix my player jumping code? 2 Answers
Character motor movement and jump 1 Answer