+1

Platformer Tutorial code

Rahul Gupta 3 years ago updated by Lazlo Bonin (Lead Developer) 3 years ago 1

Below is my code -

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class PlayerController : MonoBehaviour
{
[SerializeField]
private float Speed = 5.0f;
[SerializeField]
private float Jump = 5.0f;
float movement;
Rigidbody2D playerRigidBody2D;
Animator playerAnimator;
// Use this for initialization
void Start()
{
playerRigidBody2D = GetComponent<Rigidbody2D>();
playerAnimator = GetComponent<Animator>();
} // Update is called once per frame
void Update()
{
// calculate movement
movement = Input.GetAxis("Horizontal") * Speed;
// set velocity
playerRigidBody2D.velocity = new Vector2(movement, playerRigidBody2D.velocity.y);
// Flip move direction by scaling player on x axis
if (movement > 0)
{
transform.localScale = new Vector3(1, 1, 1);
}
else if (movement < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
playerAnimator.SetFloat("Speed", Mathf.Abs(movement));
if (GroundCheck())
{
PlayerJump();
}
playerAnimator.SetBool("Grounded", GroundCheck());
} public bool GroundCheck()
{
RaycastHit2D ground = Physics2D.CircleCast(
transform.position, // origin
0.3f, //Radius
new Vector2(0, -1), // Direction
1.1f, //Distance
LayerMask.NameToLayer("Platforms") // layer mask
);
if (ground.collider != null)
{
return false;
}
return true;
} void PlayerJump()
{
if (Input.GetButtonDown("Jump"))
{
playerRigidBody2D.AddForce(new Vector2(0f, Jump), ForceMode2D.Impulse);
}
}
}


Player still jumps even if in air.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float Speed = 5.0f;
[SerializeField]
private float Jump = 5.0f;
float movement;
Rigidbody2D playerRigidBody2D;
Animator playerAnimator;
[SerializeField]
LayerMask platforms;
// Use this for initialization
void Start()
{
playerRigidBody2D = GetComponent<rigidbody2d>();
playerAnimator = GetComponent<animator>();
print(LayerMask.NameToLayer("Platforms"));
}
// Update is called once per frame
void Update()
{
// calculate movement
movement = Input.GetAxis("Horizontal") * Speed;
// set velocity
playerRigidBody2D.velocity = new Vector2(movement, playerRigidBody2D.velocity.y);
// Flip move direction by scaling player on x axis
if (movement > 0)
{
transform.localScale = new Vector3(1, 1, 1);
}
else if (movement < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
playerAnimator.SetFloat("Speed", Mathf.Abs(movement)); // set walk animation
if (OnGround() && Input.GetButtonDown("Jump"))
{
Debug.Log("I m in");
// if (Input.GetButtonDown("Jump"))
// {
playerRigidBody2D.AddForce(new Vector2(0f, Jump), ForceMode2D.Impulse);
// }
}
}
public bool OnGround()
{
RaycastHit2D ground = Physics2D.CircleCast(
transform.position, // origin
0.3f, //Radius
new Vector2(0, -1), // Direction
1.1f, //Distance
platforms // layer mask
);
return (ground.collider != null);
}
void PlayerJump()
{
if (Input.GetButtonDown("Jump"))
{
playerRigidBody2D.AddForce(new Vector2(0f, Jump), ForceMode2D.Impulse);
}
Debug.Log(OnGround());
}
}</animator></rigidbody2d>
Bolt Version:
Unity Version:
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):

Hi Rahul, welcome to the community!

I moved your comment from the platformer tutorial to a separate topic to keep the comment thread cleaner other users. Also, please use code formatting when posting large amounts of code!