0% found this document useful (0 votes)
2 views3 pages

Unity2D Gameplay Notes

Uploaded by

Moises Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Unity2D Gameplay Notes

Uploaded by

Moises Sanchez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unity Basic 2d Gameplay Code Tutorial:

Ch 1.) Setting the Character:

1 Forming Character

We first start off with creating a Gameobject, and labeling it the player. We then attach
a sprite to this gameobject we can have something to look at and visual reference. Next
so our character can utilize Unity’s basic physics, and so we can perform basic task to
our character we now attach a Rigidbody2D, and a BoxCollider2D using add
component.

Note: We used a box collider 2d since it simply work easier than the circle collider
assuming we are working with 2d sprites

We can adjust the size of the box collider to match the dimensions our character sprite,
and so our hitbox is correct.

2 Creating Character Movement

No so we can make our character move, we attach a character movement script onto
our character. To do this we can use add component then create a new script called
playerMovement. We want to capture the input for our x and y axis. To do this we can
use Unity base input manager.

Code Skeleton:
All the basic Unity standard call in here​

[SerializeField] //this allows use to use private in inspector​
float moveSpeed;​

Vector2 inputDir; //this uses a function called vector2 which is a 2d
vector ​
Rigidbody2D rb;​

void Start()​
{​
Rb = GetComponent<RigidBody2d>();​
}​

void Update()​
{​
inputDir = new
Vector2(Input.GetAxisRaw("Horizontal"),Input.GetAxisRaw("Vertical"));​

}​

private void FixedUpdate //this helps smooth out animation​
{​
rb.velocity = inputDir* moveSpeed;​
}

On our player change the rigidbody2d component bodytype to dynamic so we can


remove the gravity component by setting gravity to zero and locking rotation by freezing
z affecting our player. Now adjust our moveSpeed so we get a smooth moving speed.

3 Camera Movement (similar to Enter the Gungeon)

All Unity stuff preload stuff

[SerializeField]
Transform target; //this will help move the camera around
[SerializeField]
float smoothSpeed;

Vector3 refVelocity;

void Start()
{
}

private void FixedUpdate() //will update after all the physics, to help get camera feel
{
transform.position = Vector3.Lerp(transform.position, new
Vector3(target.position.x,target.position.y,-10), smoothSpeed)

4 Creating Background

Create a tilemap. Add another tile map called collideObjects within our Grid. Add
composite collider (creates a large combined collider so performance sake )so we can
hit the objects and phase through. To remove the static friction, create a
PhysicsMaterial and set friction to, then attach it to our collideObjects.

-​ Cam follow !!!!

You might also like