I’m making this simple level where all you have to do is get to the flag as a cube. More to come, but for now i’m having issues with mechanics.
I want it to only jump when on the ground (No inf jumping) and I also don’t want it to be super choppy. transform.position really makes it choppy. I can move left and right, but I cannot jump
Also a couple questions, how should I set up my hit detection for 2D maps such as these? Am I doing it right? First time attempting a 2D game so i’m not sure if i’m approaching it correctly. Also, is there anyway to keep the player from clipping into the wall when moving towards it? I realize that this is due to frame updating. I just don’t know how to make smooth detection.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public float speed = 5.0f;
public float jumpHeight = 2.0f;
public bool isDead;
void Start ()
{
}
void FixedUpdate ()
{
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.position -= transform.right * speed * Time.deltaTime;
}
else if (Input.GetKey (KeyCode.RightArrow))
{
transform.position += transform.right * speed * Time.deltaTime;
}
}
// OnCOllisionStay2D for 2D box collision detection
void OnCollisionStay2D(Collision2D col)
{
if (col.gameObject.tag == "Spike")
{
Destroy (gameObject);
}
}
void OnCollisionStay2D2(Collision2D other)
{
if (other.GameObject.tag == "Floor" && Input.GetKeyDown(KeyCode.Space))
{
GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpHeight), ForceMode2D.Impulse);
}
}
}
Tried, still wanting to clip into the edges. Kinda like its bouncing off of it the more you hold in left (If you’re running into a wall thats left, vice versa)
The debug log tells me that left and right are working, but space/jump is not.
Also it’s not moving me anywhere when apporaching it like this. I’m trying to get rid of the clipping when hitting a wall.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public float speed = 5.0f;
public float jumpHeight = 10.0f;
public bool isDead;
private Rigidbody2D myRigid;
void Death ()
{
if (isDead == true)
{
Destroy (gameObject);
}
}
void Start ()
{
myRigid = GetComponent<Rigidbody2D> ();
}
void Update ()
{
if (Input.GetKey (KeyCode.LeftArrow))
{
myRigid.velocity = new Vector2(0,-10) * speed * Time.deltaTime;
Debug.Log ("I should be going left!");
}
else if (Input.GetKey (KeyCode.RightArrow))
{
myRigid.velocity = new Vector2(0,10) * speed * Time.deltaTime;
Debug.Log ("I should be going right!");
}
}
// OnCOllisionStay2D for 2D box collision detection
void OnCollisionStay2D(Collision2D col)
{
if (col.gameObject.tag == "Spike")
{
Death();
Debug.Log ("I should be dead!");
}
}
void OnCollisionStay2D2(Collision2D other)
{
if (other.gameObject.tag == "Floor" && Input.GetKey(KeyCode.Space))
{
myRigid.velocity = Vector2.up * jumpHeight * Time.deltaTime;
Debug.Log ("I should be jumping!");
}
}
}
To prevent this wall clipping, your gonna need to somehow check if your blocked before you go adding velocity to the object. Otherwise it’ll do just what your telling it to, and keep trying to add velocity no matter what if a key is pressed. Maybe you could raycast or something depending how your game is set up.
I don’t see any code regarding input from space doing anything in that snippet or I’d chime in on that…
About it not moving it now, could be that it’s just not enough velocity being added to make it move. Use a multiplier and crank it up and see what happens.
About your nickname - do you happen to actually use kali linux with unity? I’ve been curious if that was working well
There is no Unity function called “OnCollisionStay2D2”, so it will never run. In any case, input should be done in Update, since OnCollisionStay runs on the same frequency as FixedUpdate.
Hmmm ill give it a go and see if a multiplier will help at all
And about jumping, its on the bottom. But as Eric pointed out, I cant do it that way.
How can I detect if my player is touching the ground then?
Regarding my user, I have yet to try it out on Linux! I’m just a huge fan of Kali and try to do most work on it. Unity tho is something ill probably try in the near future
In case it helps, you might want to check out my article on 2D animation methods. It shows running, jumping, even double-jumping, with in-air influence (i.e. you can jump higher by holding the jump button longer, and you can influence your left-right direction while in the air too).
But note that I don’t use the physics engine… for the purposes of this article, I just have a fixed Y value for the ground. But when I’ve used similar code in real projects, I just cast a ray downward from the character’s knees or so, to see where they hit any ground collider.
I find that getting fine-grained, Mario-style control over a character is much easier when doing the physics yourself versus using the physics engine. Of course your mileage may vary.