• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Arnold // Golden Donkey Productions
      Hi, I've been working on this issue for a while and haven't yet found an answer.
      Does anyone know the best way to convert unity's LAT & LONG into a vector 3 position that I could use in a virtual world (if it's even possible). 
      Thankyou in advance
    • By wobbegong_
      I am taking an absolute beginner's game development course and we have just finished game jams in small groups. Our current assignment is to get feedback from people working in any aspect of game development. I would very much appreciate any feedback! The game is up on itchi.io (sound warning) https://wobbegong.itch.io/zombie-shooter It's essentially a very basic PvE.
      I also have some things I'm wondering about, but you don't necessarily have to answer these. 
      1. Do you have any tips on working with physics? My group wrestled a bit with Rigidbody physics not totally working the way we wanted to -- jumping ended up kind of floaty and inclines seem to mess up movement. Alternatively... how can I build terrains with depth that won't result in wonky physics?
      2. How can I keep up the level of challenge in an interesting way as the player progresses through the waves?
      3. What are some of your personal guidelines for creating title screens?
      Thank you very much in advance!
    • By ethancodes
      I'm having a weird issue with detecting a collision. I've tried everything I could find online but nothing seems to work. I have a brick object. It has a 2D Collider attached and I have also attached a 2D Rigidbody on it. I also have an EndScreen 2D Collider. The EndScreen 2D collider is tagged with "EndScreen". I am trying to detect when a brick collides with the end screen collider and simply print "game over" in the console. 
      This is my current code for this part of the program, it is attached to the bricks:
      void OnCollisionEnter (Collision2D collision) { if (collision.gameObject.tag == "EndScreen") { Debug.Log("Game over"); } } Several things have happened depending on the set up. If I have the rigidbody 2D set as static, my ball object can still collide with the bricks, but I get no Log message. If I set it to Kinematic or Dynamic, I get absolutely no interaction between the ball and the bricks, and nothing when the bricks pass through the collider. I have tried to set the collider to a trigger and use OnTriggerEnter2D, no change. I have tried to put the rigidbody on the EndScreen object and tried to set it's body type to all 3 settings, no change. The only thing I can think of that I have not done is put the script on the EndScreen object and switch the tag to the bricks. The reason I have not done this is because I will have several types of bricks, some of which will have different tags. 
       
      Please tell me somebody can see what I'm doing wrong here, because I'm losing my mind over something I feel should be ridiculously simple. Thanks.
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
    • By Sandman Academy
      Downloadable at:
      https://virva.itch.io/sandman-academy
      https://gamejolt.com/games/sandmanacademy/329088
      https://www.indiexpo.net/en/games/sandman-academy
      https://www.gamefront.com/@sandmanacademy
      http://www.indiedb.com/games/sandman-academy
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity Unity C# Jump Code Was Working. Now It's Not.

This topic is 867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,

So I wrote this based on some guides/tutorials on the internet. Pretty proud that I could understand all of the code I wrote. Anyways, I got to making the 'jump' system work. I then attempted to add a way to make it so the player could not jump an infinite amount of times. I didn't finish, and screwed up a few times. I used a backup of the code and copied + pasted it in. Now it's suddenly not working. A + D movement works fine, but SPACE does not cause the player to jump. Please help:

using UnityEngine;
using System.Collections;
 
public class PlayerController : MonoBehaviour
{
 
    public float moveSpeed = 5;
    public float jumpHeight = 100;
    public Rigidbody2D rigidBody;
 
    // Use this for initialization
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
        {
            transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
        }
 
        if (Input.GetKeyDown(KeyCode.W))
        {
            rigidBody.AddForce(new Vector2(0, jumpHeight));
        }
    }
}

Code: http://pastebin.com/K8iftHTx

Edited by Ovicior

Share this post


Link to post
Share on other sites
Advertisement

Alright, I figured it out.

 

Never make your variables in Unity public. The public settings were taking priority over the code, messing everything up.

Share this post


Link to post
Share on other sites

Public variables are there to allow artists to set up values on editor, your rigidbody has no sense to be public since it's only used for this class.

Share this post


Link to post
Share on other sites

It could've been that his jumpheight was set to 0 as a public variable, or some low number, or the mass on the object was changed, so the jumpheight was then not a sufficient enough force to cause the player to jump.

 

Depending on your game, I kind of like doing AddForce and adding the mass into the equation ( so AddForce( someForce * rigidbody.Mass), so that if I tweak the mass for some other reason, my jump height stays relatively stable.  Though in my case it was for cannon shell lobbing, I wanted them to have greater impact on their targets, but every time I tweaked them, the shells would travel slower, which I didn't want.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement