Unity Basic 2d platformer without RigidBody

I wanted to do 2d platformer ( super mario style) without using unitys own rigidbody. So for collision detection I used Raycast and LineCast.
First make a cube, put it to 0x,0y,0z and attach camera to it. Then set projection to orthographic and scale size how much you want, I used 13. Then make a floor from cube. Make sure that everything you add to the scene has same Z value on their transform.

Movement and Collision detection

In script basically we make player to raycast and look if there is anything under, infront or back of it and if there is, we either disable movementkeys so player can’t go throught walls or we make sure that our player doesn’t walk throught objects. In this script you can jump throught objects from under it. It’s working as intended, becouse I wanted that kind of behavior in my game.

Here is player movement/collision detection script in C#.

using UnityEngine;
using System.Collections;

public class playermovement : MonoBehaviour {
        /*our speeds*/
		public int walkSpeed = 10;
		public int runSpeed = 20;
		public int jumpSpeed = 15;
		public int fallingSpeed = 3;

       /*isAir = Is player in air?*/
		public bool inAir;
       /*Is right/left arrowkey useable?*/
		public bool rightArrowKey;
		public bool leftArrowKey;

	public void Start()
		{
		Time.timeScale = 0.5f;
/*In start we set inAir to false and our keys to useable*/
		inAir = false;
		rightArrowKey = true;
		leftArrowKey = true;
		}
	public void Update () {
/*Check if Right arrow key is pressed and its useable*/
		if(Input.GetKey(KeyCode.RightArrow) && rightArrowKey == true)
			{       /*Checks if shift is pressed at the same time then it moves * runSpeed instead of walkSpeed. If not it will move by walkspeed*/
				if(Input.GetKey(KeyCode.LeftShift))
				{
					transform.Translate(Vector3.right * runSpeed * Time.deltaTime);
				}
				else
				{
					transform.Translate(Vector3.right * walkSpeed * Time.deltaTime);
				}

			}
/*Same to left arrow key and movement*/
		if(Input.GetKey(KeyCode.LeftArrow) && leftArrowKey == true)
			{
				if(Input.GetKey(KeyCode.LeftShift))
				{transform.Translate(Vector3.left * runSpeed * Time.deltaTime);
				}
				else
				{
					transform.Translate(Vector3.left * walkSpeed * Time.deltaTime);
				}
			}
/*Checks if space is pressed*/
	if(Input.GetKey(KeyCode.Space) && inAir == false)
		{
/*moves player up * jumpSpeed*/
                transform.Translate(Vector3.up * jumpSpeed * Time.deltaTime);
/*Starts Ienumerator jump*/
		StartCoroutine("jump");
		}
/*Check if player collides anything under it*/
     if (Physics.Raycast(transform.position,Vector3.down,transform.localScale.y / 2) || Physics.Raycast(transform.position - new Vector3(transform.localScale.x /2,0,0),Vector3.down,transform.localScale.y / 2) || Physics.Raycast(transform.position + new Vector3(transform.localScale.x / 2,0,0),Vector3.down,transform.localScale.y / 2))
		{
/*inAir is then set to false*/
			inAir = false;

		}
/*if raycast dsnt collide anything under player it must be in ground so we set it to start falling down*/
		else
		{
			walkSpeed = 10;
			runSpeed = 15;
			fallingSpeed = 15;
			transform.Translate(Vector3.down * fallingSpeed * Time.deltaTime);
		}
/*checks if there is anything infront of player and disables right arrow key so player cant move throught objects*/
		if (Physics.Raycast(transform.position,Vector3.right,transform.localScale.y /2))
		{
			//Debug.Log ("something infront of player");
			rightArrowKey = false;

		}
/*If nothing is infront of player you can continue moving on*/
		else
		{
			rightArrowKey = true;
		}
/*Same to left side*/
		if (Physics.Raycast(transform.position,Vector3.left,transform.localScale.y /2))
		{
			//Debug.Log ("something infront of player");
			leftArrowKey = false;

		}
		else
		{
			leftArrowKey = true;
		}

	}
/*Coroutine that we start if we jump. It will wait one millisecond and then it realizes its in air and starts falling down, this is done so it doesn't start to fall down same time as it jumps*/
	IEnumerator jump()
	{
		yield return new WaitForSeconds(0.1f);
		inAir = true;
	}
}

(Green is player)
2dgame

Next step simple enemies

Next we need to make enemies. I decided to make really simple enemies which run towards left untill they detect object infront and then they check, if object is player, if it is, game ends if it’s not they turn around and go to other way. If enemy doesn’t find anything under it, it will start to fall down. Also I made that if enemy finds anything above it , enemy will be deleted out from scene. So it works like goombas in supermario.

Enemy Script:

sing UnityEngine;
using System.Collections;

public class enemy : MonoBehaviour {

public bool move;
     void Start()
        {
        move = false;
        }
	void Update () {
/*Stores details about raycasthit*/
		RaycastHit hit;
/*If raycast finds anything above enemy destroy the enemy*/
	if (Physics.Raycast(transform.position,Vector3.up,transform.localScale.y / 2))
		{
			Destroy(gameObject);
		}
/*If raycast finds anything infront of enemy and if that object has tag wich is "Player" end the game. If it's not the player that is infront of enemy it will just turn around.*/
	if(Physics.Raycast(transform.position,Vector3.left,out hit,transform.localScale.x /2))
		{
			if(hit.collider.gameObject.tag == "Player")
			{
			Application.LoadLevel("Your lvl here");
			}
		}
/*Same to other side*/
	if(Physics.Raycast(transform.position,Vector3.right,out hit,transform.localScale.x /2))
		{
			if(hit.collider.gameObject.tag == "Player")
			{
			Application.LoadLevel("Your lvl here");
			}
		}
/*if enemy finds anything right side of it we set boolean move to false*/
if (Physics.Raycast(transform.position,Vector3.right,transform.localScale.y /2))
		{
			move = false;
		}
/*if enemy finds anything from left side of it we set boolean move to true*/
	else if(Physics.Raycast(transform.position,Vector3.left,transform.localScale.y /2))
		{
			move = true;
		}
/*if boolean move = true our enemy starts to move to right*/
	if(move == true)
		{
			transform.Translate(Vector3.right * 7 * Time.deltaTime);
		}
/*if boolean move = false our enemy starts to move to left*/
	if(move == false)
		{
			transform.Translate(Vector3.left * 7 * Time.deltaTime);
		}
/*if enemy finds anything under it do nothing*/
	if (Physics.Raycast(transform.position,Vector3.down,transform.localScale.y / 2) || Physics.Raycast(transform.position - new Vector3(transform.localScale.x / 2,0,0),Vector3.down,transform.localScale.y / 2) || Physics.Raycast(transform.position + new Vector3(transform.localScale.x / 2,0,0),Vector3.down,transform.localScale.y / 2))
		{

		}
/*if enemy doesn't find anything under it, it will start to fall down*/
		else
		{
			transform.Translate(Vector3.down * 15 * Time.deltaTime);
		}
	}

}

Then we can make simple script that spawns enemies on certain spots. We have a timer that starts to decrease and when it hits 0 it spawns an enemy on spot and then timer goes back to start. To make this work you need to drag one enemy from your hierarchy to your assests folder and it will become prefab. Then in inspector insert this code to player or anything that doesn’t get removed from scene. In inspector drag your enemy prefab from assest folder to None GameObject slot on your code attached to the player or anywhere you attached it.

using UnityEngine;
using System.Collections;

public class enemySpawn : MonoBehaviour {
/* Our prefab*/
	public GameObject prefab;
/*Our timer*/
	public float timer = 2;

	void Update () {
/*Checks if timer is 0*/
		if(timer == 0)
		{
/*Spawns enemy from prefab to spot 126x,78y,0z*/
			Instantiate(prefab,new Vector3(126,78,0),Quaternion.identity);
/* You can add multiple spots here*/
/*Sets timer back to 2*/
			timer = 2;
		}
/*Decreases timer by deltatime*/
		timer -= Time.deltaTime;
/*Checks if timer is below 0 and sets it back to 0 if it is*/
		if(timer <= 0)
		{
			timer = 0;
		}

	}
}

(Green Player, Yellow Enemies)
2dgame2

Next we need something that kills player and deletes every enemy that drops down from our lvl.

I made it by using linecast and attached it to a cube that I scaled under the scene. If anything hits that line it will be deleted or if player hits it game will start again.

Code:

using UnityEngine;
using System.Collections;

public class death : MonoBehaviour {

	void Update () {
		/*stores raycast hit info*/
		RaycastHit hit;
		/* makes line equal to the object you make and checks if anything hits it*/
		if(Physics.Linecast(transform.position + new Vector3(-transform.localScale.x / 2,transform.localScale.y / 2,0),
			transform.position + new Vector3(transform.localScale.x / 2,transform.localScale.y / 2,0),out hit))
		{
/*Checks if object that hits line has playermovenet script attached to it*/
			if(GetComponent<playermovement>())
			{
/*if yes restart scene*/
				Application.LoadLevel("Your lvl here");
			}
/*if not destroy it. Enemies will not have playermovement script atatched to them so they will be destroyed if they hit the line*/
			Destroy(hit.collider.gameObject);
		}
	}
}

(Player, Enemy and yellow line of death)
2dgame3

Now last thing we need is a goal.

We need to make object that checks if it finds anything infront of it that has tagg “Player”. If yes level is completed. I made REALLY simple script to do that.

using UnityEngine;
using System.Collections;

public class goal : MonoBehaviour {

	void Update () {
	//Stores raycast hit info
		RaycastHit hit;
/* checks if there is something in let side of object*/
		if(Physics.Raycast(transform.position,Vector3.left,out hit,transform.localScale.x /2))
		{
/*checks if it has tag "Player" in it*/
			if(hit.collider.gameObject.tag == "Player")
			{
/*if yes load your victory screen here or something you want*/
			print("Victory);
			}
		}
	}
}

(Player left side smaller green cube and goal rigth side bigger cube)
2dgame4

I Hope, this helps someone, who wants to do a simple 2d platformer. I’m just a student myself so, if you find some horrible bugs or better ways of doing certain things, please let me know 🙂

Lightning Strike on Unity 3d Using Line renderer.

What do we need to do it?

We need to use unity build in feature called LineRenderer wich draws line between two objects.

We set it to draw line between our 1st person controller and its target in this case red cube in middle. Then we make randomised path to our line.  Then we make loop when we push mouse button 0 aka right mouse click 1st person controller will shoot lightning on red cube. Ofc to make line look like lighting you will have to use texture and change color. I decided to use unitys own textures and selected blue color for my lightning. You can add texture and color from inspector.

 Full code:

var targetObject : GameObject;
private var lineRenderer : LineRenderer;
var asd : int = 3;
function Start()
{

    lineRenderer = GetComponent(LineRenderer);
}

function Update ()
{
//Check if we press right mouse button
	if(Input.GetKey(KeyCode.Mouse0))
	{
//Enables line renderer
		lineRenderer.enabled = true;
 //Sets starting position to players position
			lineRenderer.SetPosition(0,this.transform.localPosition);
//Loop that moves line to "random directions"
	    	for(var i:int=1;i<4;i++)
	    	 {
	        	var pos = Vector3.Lerp(this.transform.localPosition,targetObject.transform.localPosition,i/4.0f);

                        //randomises lines position
	        	pos.x += Random.Range(-0.4f,0.4f);
	        	pos.y += Random.Range(-0.4f,0.4f);

	        	lineRenderer.SetPosition(i,pos);
	   		}
//Lines end postion at the target
	    	lineRenderer.SetPosition(4,targetObject.transform.localPosition);
	}
    else
    {
// If we dont press right mouse button disables linerenderer
    	lineRenderer.enabled = false;

    }
}

Lightning bolt

Based on this tutorial