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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple beginer platformer

Discussion in 'Scripting' started by Sirevix, Mar 18, 2015.

  1. Sirevix

    Sirevix

    Joined:
    Mar 18, 2015
    Posts:
    10
    Hello
    So i was waching this video /watch?v=qwuPiaFU37w
    and this guy is making a game
    but i was making my own game with his instructions
    is something like a platformer ... well for the moment i only have one plarfrm and a cube that is my character and made him move

    This is my character controller script

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.     public float moveSpeed;
    7.     private float maxSpeed = 5f;
    8.  
    9.     private Vector3 input;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.     }
    16.  
    17.     void Update () {
    18.         input = new Vector3 (Input.GetAxisRaw ("Horizontal"),  Input.GetAxisRaw ("Vertical"),0);
    19.         if(GetComponent<Rigidbody>().velocity.magnitude < maxSpeed)
    20.  
    21.  
    22.         {
    23.             GetComponent<Rigidbody>().AddForce(input * moveSpeed);
    24.         }
    25.  
    26.  
    27.     }
    28. }
    29.  
    30.  
    he moves right, left, up, down.
    when is in middle air you can press down so he falls down quicker

    my preoblem is that if i press UP the cube go's up forever... well untill i release the key UP and then starts falling again

    have any ideas how to make him jump only once or.. doble jump

    Anything to meke him stop from ascending infinitely :-|

    i don't know if i explayned myself correctly
    And sorry for bad Engish :-|
     
    Last edited: Mar 18, 2015
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    ya you will have to implemeant a ground check.

    also please properly use the Code Tags

    A easy way to check if your charcter is grounded is add a new empty gameObject to your charcter, and place it under his feet, than use a pieac of code like this.
    Code (CSharp):
    1. _grounded = Physics2D.OverlapCircle(GroundCheck.position, GroundRadius, GroundLayers);
    than put your jump code in a if statemeant that checks for _grounded, and only allows a jump if true. What this is doing is doing a checking to see if any gemotry is overlapping a transform you got under your players feet, and if yes it is setting grounded to true, which will allow you to jump, but while in the air _grounded would be false and block jumping. Also since jump is a special circumstance i would not have it as part of your input vector, but move its logic to its own method.

    Also your are wasting lots of resources by doing a GetComponent on RigidBody every Update, you should do the GetComponent in Start and just store it to a variable for later
     
    Last edited: Mar 18, 2015
    Sirevix and TheValar like this.
  3. Sirevix

    Sirevix

    Joined:
    Mar 18, 2015
    Posts:
    10
    where i have to insert this
    Code (csharp):
    1.  _grounded = Physics2D.OverlapCircle(GroundCheck.position, GroundRadius, GroundLayers);
    ??

    oh and also...
    it works with w,a,s,d too

    and if the D key is pressed i can't press W at the same time

    Should i start with an easyer game for my first game/script/everything ?
    it's the easyest game that come to my mind
     
    Last edited: Mar 18, 2015
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    you would have to change how you implemeant it all, im just saying that is the line of code that would do the groundCheck and decide if you can jump or not