Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How Can I Make My Script Make My Player Jump?

Discussion in 'Scripting' started by VoicefulAuto78, Feb 20, 2020.

  1. VoicefulAuto78

    VoicefulAuto78

    Joined:
    Jan 29, 2020
    Posts:
    2
    Ok, so I'm pretty new to this whole scripting business and game making. So far I have a testing level and what not to test my scripts and other things. One problem I have constantly run into is trying to script the player jump control. So far I can get the player to move horizontally on the x-axis, but can't get it to jump on the y-axis.

    Here is my script so far:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController2 : MonoBehaviour
    6. {
    7.    
    8.     public float speed = 5.0f;
    9.     public float jumpSpeed = 5.5f;
    10.     public LayerMask groundLayer;
    11.  
    12.     public Transform groundCheck;
    13.     private float scaleX = 1.5f;
    14.     private float scaleY = 1.5f;
    15.  
    16.     public float timeBetweenJumps = 0.5f;
    17.     public float timeTilNextJump = -1;
    18.  
    19.  
    20.     private void Start()
    21.     {
    22.  
    23.     }
    24.  
    25.  
    26.     void FixedUpdate()
    27.     {
    28.         //get input for horizontal movement
    29.         float h = Input.GetAxis("Horizontal");
    30.  
    31.    
    32.  
    33.         //check to see if the groundcheck object overlaps the ground layer
    34.         bool touchesGround = Physics2D.OverlapPoint(groundCheck.position, groundLayer);
    35.  
    36.  
    37.  
    38.         if (Input.GetKeyDown(KeyCode.Space)&&(Time.time>timeTilNextJump))
    39.         {
    40.             if(touchesGround)
    41.             {
    42.  
    43.                 //if it's touching the ground, give it a burst of force upwards
    44.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
    45.                // GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
    46.                 touchesGround = false;
    47.                 timeTilNextJump = Time.time + timeBetweenJumps;
    48.             }
    49.         }
    50.  
    51.         //if h is positive face right, if not, face left
    52.         if (h > 0)
    53.         {
    54.             transform.localScale = new Vector2(scaleX, scaleY);
    55.         }
    56.         else if (h < 0)
    57.         {
    58.             transform.localScale = new Vector2(-scaleX, scaleY);
    59.         }
    60.         //put it all together and feed it into the velocity component of the rigidbody to move
    61.         GetComponent<Rigidbody2D>().velocity = new Vector2(h * speed, GetComponent<Rigidbody2D>().velocity.y);
    62.     }
    63.  

    All of this code is from a tutorial from mooict.com
    https://www.mooict.com/unity-2d-platformer/

    Let me know where I went wrong or haven't done yet to make this work.

    P.S
    I've been getting a build error in the Unity editor telling me that groundCheck hasn't been assigned, but I don't know how to get it assigned. Sorry if this code is absolute garbage. Still learning C# on the game development level.
     
  2. kensarto17

    kensarto17

    Joined:
    Nov 11, 2017
    Posts:
    27
    because you've made groundCheck a transform, i will assume it is the transform of the ground object in your game.
    to assign it, what you need to do is go through the editor itself, not your script. Go to where you placed this script, wherever that may be, likely on your player object, then drag the ground object into the part of that script that is asking you to place the groundCheck transform.