- Home /
 
 
               Question by 
               multiverse3snowflake · Jun 15, 2020 at 09:35 AM · 
                2d-platformerjump2d-physics2d-sidescroller  
              
 
              How do I get the character to jump in a 2d game, which is compatible with joysticks and keyboards?
I'm having trouble getting this character to jump, this is my first 2d project using unity. I tried using a different code with rigidbody 2d, but the character was rotating when hitting an edge. The character can move horizontally but not vertically. I also tried setting a limit so the character doesn't pass through the floor. i have everything labeled such as floor colliders and so on. Can someone explain my issue?
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NightmareController : MonoBehaviour
 {
     public CharacterController controller;
 
     public float speed;
     public float jumpHeight;
     public float gravity = -9.81f;
 
     public Transform groundCheck;
     public float groundDistance = 0.4f;
     public LayerMask groundMask;
 
     Vector3 velocity;
 
     bool isGrounded;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
 
         float x = (Input.GetAxis("Horizontal"));
         float y = (Input.GetAxis("Vertical"));
 
         if (isGrounded && velocity.y < 0)
         {
             velocity.y = -2f;
         }
 
         if (y < 0)
         {
             y = 0;
         }
         else if (y > 0 && isGrounded)
         {
             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
         }
         Vector3 move = transform.right * x + transform.forward * y;
 
         velocity.y += gravity * Time.deltaTime;
 
         controller.Move(move * speed * Time.deltaTime);
 
     }
 }
 
              
               Comment
              
 
               
              Your answer