Search Unity

Character not jumping

Discussion in '2D' started by LasersharkStudios, May 22, 2020.

  1. LasersharkStudios

    LasersharkStudios

    Joined:
    May 22, 2020
    Posts:
    2
    this is my code for my Charater and when I run it i can move but I it does not jump can someone plase help, the animations for idle and run work fine but I cannot jump

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playercontroller : MonoBehaviour {
    6.  
    7.     public Animator animator;
    8.  
    9.     public float topspeed = 10f;
    10.         bool facingRight = true;
    11.  
    12.     bool grounded = false;
    13.  
    14.         public Transform GroundCheck;
    15.  
    16.     float groundRadius = 0.2f;
    17.  
    18.     public float jumpForce =700f;
    19.  
    20.     public LayerMask whatIsGround;
    21.  
    22.  
    23.  
    24.     void start ()
    25.     {
    26.         animator = GetComponent<Animator> ();
    27.    
    28.    
    29.     }
    30.  
    31.  
    32.     void FixedUpdate()
    33.     {
    34.    
    35.         grounded = Physics2D.OverlapCircle (GroundCheck.position, groundRadius, whatIsGround);
    36.         animator.SetBool("Ground", grounded);
    37.  
    38.         animator.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
    39.  
    40.         float move = Input.GetAxis ("Horizontal");
    41.  
    42.  
    43.  
    44.    
    45.     GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * topspeed, GetComponent<Rigidbody2D> ().velocity.y);  
    46.  
    47.         animator.SetFloat ("Speed", Mathf.Abs (move));
    48.  
    49.         if (move < 0 && !facingRight)
    50.             Flip ();
    51.         else if (move >  0 && facingRight)
    52.             Flip();
    53.  
    54.    
    55.     }
    56.  
    57.     void Update()
    58.     {
    59.         if (grounded && Input.GetKeyDown (KeyCode.Space))
    60.         {
    61.             animator.SetBool ("Ground", false);
    62.  
    63.          GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpForce));
    64.        
    65.         }
    66.     }
    67.  
    68.  
    69.     void Flip()
    70.     {
    71.    
    72.         facingRight = !facingRight;
    73.  
    74.         Vector3 theScale = transform.localScale;
    75.  
    76.         theScale.x *= -1;
    77.  
    78.         transform.localScale = theScale;
    79.    
    80.    
    81.     }
    82.  
    83. }
     
  2. LasersharkStudios

    LasersharkStudios

    Joined:
    May 22, 2020
    Posts:
    2
    This has been resolved, sorry moderators you don't have to pass this through b