Basics of Unity: 2D Platformer Character Controller (i've finally made something a little more useful than raw unity info)
Here the the repo if youre just after the code (its all free to use)
https://github.com/superbird29/2DCharacterController
The Video
https://www.youtube.com/watch?v=ZVDbjhmdWgY
Here is the tutorial video where I walk you through making a 2D platformer character controller. It's pretty long (26 mins) since I do lots of explaining on the what something does, a little explaining on the why and I cover lots of my own pitfalls. It's also heavily chaptered to all skipping of parts you dont need!
Also it is very important to catch any pitfalls so if you see any let me know.
Let me know if you have any questions or concerns?
If you want a shorter one that just walks you through the code and doesnt opine as much on how to actually do it just let me know below!
I am also taking suggestions for future videos
https://redd.it/1gz40o0
@r_Unity3D
Here the the repo if youre just after the code (its all free to use)
https://github.com/superbird29/2DCharacterController
The Video
https://www.youtube.com/watch?v=ZVDbjhmdWgY
Here is the tutorial video where I walk you through making a 2D platformer character controller. It's pretty long (26 mins) since I do lots of explaining on the what something does, a little explaining on the why and I cover lots of my own pitfalls. It's also heavily chaptered to all skipping of parts you dont need!
Also it is very important to catch any pitfalls so if you see any let me know.
Let me know if you have any questions or concerns?
If you want a shorter one that just walks you through the code and doesnt opine as much on how to actually do it just let me know below!
I am also taking suggestions for future videos
https://redd.it/1gz40o0
@r_Unity3D
GitHub
GitHub - superbird29/2DCharacterController
Contribute to superbird29/2DCharacterController development by creating an account on GitHub.
Text is not appearing when i open Unity, is there any way to solve this?
https://redd.it/1gz4vyn
@r_Unity3D
https://redd.it/1gz4vyn
@r_Unity3D
Platformer walljumping when I don't want it to
So I'm kinda new at this and trying to make a simple platformer noscript and I've gotten pretty close to what I want. Issue here is that when the character touches a wall instead of falling it just levitates midair, which allows for it to basically save from every fall by walljumping. Help?
{
//Movement
public float speed;
public float jump;
float moveVelocity;
public Rigidbody2D rb;
bool isGrounded;
void Update()
{
//Grounded?
if (isGrounded == true)
{
//jumping
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
}
}
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
moveVelocity = -speed;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveVelocity = speed;
}
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
}
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("OnCollisionEnter2D");
isGrounded = true;
}
void OnCollisionExit2D(Collision2D col)
{
Debug.Log("OnCollisionExit2D");
isGrounded = false;
}
}
https://redd.it/1gz7vsi
@r_Unity3D
So I'm kinda new at this and trying to make a simple platformer noscript and I've gotten pretty close to what I want. Issue here is that when the character touches a wall instead of falling it just levitates midair, which allows for it to basically save from every fall by walljumping. Help?
{
//Movement
public float speed;
public float jump;
float moveVelocity;
public Rigidbody2D rb;
bool isGrounded;
void Update()
{
//Grounded?
if (isGrounded == true)
{
//jumping
if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Z) || Input.GetKeyDown(KeyCode.W))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
}
}
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
moveVelocity = -speed;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveVelocity = speed;
}
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
}
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("OnCollisionEnter2D");
isGrounded = true;
}
void OnCollisionExit2D(Collision2D col)
{
Debug.Log("OnCollisionExit2D");
isGrounded = false;
}
}
https://redd.it/1gz7vsi
@r_Unity3D
Reddit
From the Unity2D community on Reddit
Explore this post and more from the Unity2D community
Pipe system
So,, my character is liquid and i was thinking to add a pipe system that the character can go inn and go to different parts of the map, i just don't know how to do it, any ideas?
https://redd.it/1gz5my2
@r_Unity3D
So,, my character is liquid and i was thinking to add a pipe system that the character can go inn and go to different parts of the map, i just don't know how to do it, any ideas?
https://redd.it/1gz5my2
@r_Unity3D
Reddit
From the Unity2D community on Reddit
Explore this post and more from the Unity2D community
Seeking Advice on Creating a Plant Growth Mechanic Similar to Trellis in Unity
https://preview.redd.it/0qopboescz2e1.png?width=415&format=png&auto=webp&s=9f393ef668975b956e12c89de3fff1d0690038c6
This is the Game: Trellis
https://redd.it/1gzb8ht
@r_Unity3D
https://preview.redd.it/0qopboescz2e1.png?width=415&format=png&auto=webp&s=9f393ef668975b956e12c89de3fff1d0690038c6
This is the Game: Trellis
https://redd.it/1gzb8ht
@r_Unity3D
How can I make a background?
Hi, i'm a beginner on Unity and I'm sure how to make a background/map. I've seen a few tutorial about doing a tilemap, but everytime i do this, the tilemap overlap my character, so i'm not sure where i can look to make the character overlap the tilemap/background?
Btw srry for my bad english, its not my first language😅
Thx.
https://redd.it/1gz15pz
@r_Unity3D
Hi, i'm a beginner on Unity and I'm sure how to make a background/map. I've seen a few tutorial about doing a tilemap, but everytime i do this, the tilemap overlap my character, so i'm not sure where i can look to make the character overlap the tilemap/background?
Btw srry for my bad english, its not my first language😅
Thx.
https://redd.it/1gz15pz
@r_Unity3D
Reddit
From the Unity2D community on Reddit
Explore this post and more from the Unity2D community
This media is not supported in your browser
VIEW IN TELEGRAM
I put a great big Akira inspired freight elevator in my game about a quantum cat. It really helps sell the scale of the environments.
https://redd.it/1gzdnl2
@r_Unity3D
https://redd.it/1gzdnl2
@r_Unity3D
What open source projects have you been a part of? Is it still active?
Hello everybody
I've been wanting to contribute to open source projects to sharpen my skills, but haven't found anything yet. wanted to see if you guys have been a part of the game dev open source community.
would love to hear about your experience
https://redd.it/1gzi2pf
@r_Unity3D
Hello everybody
I've been wanting to contribute to open source projects to sharpen my skills, but haven't found anything yet. wanted to see if you guys have been a part of the game dev open source community.
would love to hear about your experience
https://redd.it/1gzi2pf
@r_Unity3D
Reddit
From the Unity2D community on Reddit
Explore this post and more from the Unity2D community
Media is too big
VIEW IN TELEGRAM
Made a fire stream out of symbols. Does it look good? Current battle gameplay: everything shoots automatically. You just reload, maybe craft something mid-fight (like a shield or a support drone) and escape in time. Still deciding if I should rework it to give the player more control...
https://redd.it/1gzkvwt
@r_Unity3D
https://redd.it/1gzkvwt
@r_Unity3D
2D Platform Game for School Project... NEED HELP
Hey guys, this is my code in C# that i actually got from chatGPT because im not really good in coding haha... The problem is that for player2 (frog) it is not actally reseting horizontal velocity only after landing but its like allways... and if there is no reseting of horizontal velocity the frog is sliding after landing.. Can anybody help me with that somehow?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TwoPlayerMovement : MonoBehaviour
{
Header("Player 1 Settings")
public Rigidbody2D rb1;
public Transform groundCheck1;
public LayerMask groundLayer1;
private float horizontal1;
private float speed1 = 8f;
private float jumpingPower1 = 16f;
private bool isFacingRight1 = true;
Header("Player 2 Settings (Frog)")
public Rigidbody2D rb2;
public Transform groundCheck2;
public LayerMask groundLayer2;
private bool isGrounded2;
private bool isChargingJump2 = false;
private bool wasInAir2 = false; // Detects if the frog was previously in the air
private float chargeTime2 = 0f;
private float maxChargeTime2 = 1.5f; // Maximum charge time
private float minJumpPower2 = 6f; // Minimum jump strength
private float maxJumpPower2 = 16f; // Maximum jump strength
private float jumpDirection2 = 0f; // Direction of the jump (-1 = left, 1 = right)
private bool isFacingRight2 = true;
private void Update()
{
// Player 1 Controls
horizontal1 = Input.GetAxisRaw("HorizontalP1"); // Custom axis for Player 1
if (Input.GetButtonDown("JumpP1") && IsGrounded(groundCheck1, groundLayer1))
{
rb1.velocity = new Vector2(rb1.velocity.x, jumpingPower1);
}
if (Input.GetButtonUp("JumpP1") && rb1.velocity.y > 0f)
{
rb1.velocity = new Vector2(rb1.velocity.x, rb1.velocity.y 0.5f);
}
Flip(ref isFacingRight1, horizontal1, rb1.transform);
// Player 2 (Frog) Controls
isGrounded2 = IsGrounded(groundCheck2, groundLayer2);
if (isGrounded2)
{
// If frog just landed, reset horizontal velocity
if (wasInAir2)
{
rb2.velocity = new Vector2(0f, rb2.velocity.y); // Reset horizontal velocity only after landing
wasInAir2 = false;
}
// Start charging the jump
if (Input.GetButtonDown("JumpP2"))
{
isChargingJump2 = true;
chargeTime2 = 0f;
}
// Set jump direction based on input
float inputHorizontal = Input.GetAxisRaw("HorizontalP2");
if (inputHorizontal != 0)
{
jumpDirection2 = inputHorizontal > 0 ? 1f : -1f;
isFacingRight2 = jumpDirection2 > 0;
Flip(ref isFacingRight2, jumpDirection2, rb2.transform);
}
}
// Charge the jump
if (isChargingJump2)
{
chargeTime2 += Time.deltaTime;
chargeTime2 = Mathf.Clamp(chargeTime2, 0f, maxChargeTime2); // Limit to max charge time
}
// Release the jump
if (Input.GetButtonUp("JumpP2") && isChargingJump2)
{
isChargingJump2 = false;
// Calculate jump power based on charge time
float jumpPower = Mathf.Lerp(minJumpPower2, maxJumpPower2, chargeTime2 / maxChargeTime2);
// Apply the jump
rb2.velocity = new Vector2(jumpDirection2 jumpPower, jumpPower);
// Mark that the frog is in the air
wasInAir2 = true;
}
}
Hey guys, this is my code in C# that i actually got from chatGPT because im not really good in coding haha... The problem is that for player2 (frog) it is not actally reseting horizontal velocity only after landing but its like allways... and if there is no reseting of horizontal velocity the frog is sliding after landing.. Can anybody help me with that somehow?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TwoPlayerMovement : MonoBehaviour
{
Header("Player 1 Settings")
public Rigidbody2D rb1;
public Transform groundCheck1;
public LayerMask groundLayer1;
private float horizontal1;
private float speed1 = 8f;
private float jumpingPower1 = 16f;
private bool isFacingRight1 = true;
Header("Player 2 Settings (Frog)")
public Rigidbody2D rb2;
public Transform groundCheck2;
public LayerMask groundLayer2;
private bool isGrounded2;
private bool isChargingJump2 = false;
private bool wasInAir2 = false; // Detects if the frog was previously in the air
private float chargeTime2 = 0f;
private float maxChargeTime2 = 1.5f; // Maximum charge time
private float minJumpPower2 = 6f; // Minimum jump strength
private float maxJumpPower2 = 16f; // Maximum jump strength
private float jumpDirection2 = 0f; // Direction of the jump (-1 = left, 1 = right)
private bool isFacingRight2 = true;
private void Update()
{
// Player 1 Controls
horizontal1 = Input.GetAxisRaw("HorizontalP1"); // Custom axis for Player 1
if (Input.GetButtonDown("JumpP1") && IsGrounded(groundCheck1, groundLayer1))
{
rb1.velocity = new Vector2(rb1.velocity.x, jumpingPower1);
}
if (Input.GetButtonUp("JumpP1") && rb1.velocity.y > 0f)
{
rb1.velocity = new Vector2(rb1.velocity.x, rb1.velocity.y 0.5f);
}
Flip(ref isFacingRight1, horizontal1, rb1.transform);
// Player 2 (Frog) Controls
isGrounded2 = IsGrounded(groundCheck2, groundLayer2);
if (isGrounded2)
{
// If frog just landed, reset horizontal velocity
if (wasInAir2)
{
rb2.velocity = new Vector2(0f, rb2.velocity.y); // Reset horizontal velocity only after landing
wasInAir2 = false;
}
// Start charging the jump
if (Input.GetButtonDown("JumpP2"))
{
isChargingJump2 = true;
chargeTime2 = 0f;
}
// Set jump direction based on input
float inputHorizontal = Input.GetAxisRaw("HorizontalP2");
if (inputHorizontal != 0)
{
jumpDirection2 = inputHorizontal > 0 ? 1f : -1f;
isFacingRight2 = jumpDirection2 > 0;
Flip(ref isFacingRight2, jumpDirection2, rb2.transform);
}
}
// Charge the jump
if (isChargingJump2)
{
chargeTime2 += Time.deltaTime;
chargeTime2 = Mathf.Clamp(chargeTime2, 0f, maxChargeTime2); // Limit to max charge time
}
// Release the jump
if (Input.GetButtonUp("JumpP2") && isChargingJump2)
{
isChargingJump2 = false;
// Calculate jump power based on charge time
float jumpPower = Mathf.Lerp(minJumpPower2, maxJumpPower2, chargeTime2 / maxChargeTime2);
// Apply the jump
rb2.velocity = new Vector2(jumpDirection2 jumpPower, jumpPower);
// Mark that the frog is in the air
wasInAir2 = true;
}
}