Making a 2D Game in unity part-3

Here we are going to make our player. I used a simple ball image. you can use your own. In this tutorial we will make a player conrtoller script.

I took a player image like this. 1.png

import it to your scene set the position from transform at Vector3(0,0,-5). add a polygon collider and check it matches with the size of the image. Now add a Rigidbody2D to the player.

2.PNG

Rigidbody2D handels the physics behaviour of a game object. now we are going to make a contoller script. For this game I am going to make a script which will push the ball with some force to up and right. So in play time when we tap on the screen the ball will jump to Up-Right. change its tag into "Player"

3.png

make a new c# script and name it "playerScript". Now add the script with the player and open it on mono behaviour. Copy and pest this script to your playerScript.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerScript : MonoBehaviour {
public float jumpForce;
Rigidbody2D rb;
// Use this for initialization
void Start () {
    rb = GetComponent<Rigidbody2D> ();
}

// Update is called once per frame
void Update () {
    if(Input.GetButtonDown("Fire1")){
        rb.velocity = Vector2.zero;
        rb.velocity += new Vector2(jumpForce, jumpForce);
    }
    }
}

after attaching the script with the player you will see a variable that in the inspector. Give a value there. it will control the force of the jump.

4.png

hit the play button of unity. click with left mouse on the scene button on the game scene. and the player should work fine. camera is not mooving with the ball so after some click the ball will jump out from the scene. W need a camera script so that we can make the camera follow the player. We are going to make our camera script next.

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?