Showing posts with label 2d. Show all posts
Showing posts with label 2d. Show all posts

Friday, 23 February 2018

Random tiles Generation in Unity 2d game at runtime

 Watch Video on youtube

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

public class TilesGenerator : MonoBehaviour
{
    private Camera mainCam;
    public GameObject Player;
    public GameObject[] Tiles;
    float spawnY;
    // Use this for initialization
    void Start () {
        mainCam = Camera.main;
        spawnY = mainCam.transform.position.y + 3.0f;
    }
   
    // Update is called once per frame
    void Update ()
    {
        if (mainCam.transform.position.y < Player.transform.position.y)
        {
            Vector3 targetPos = new Vector3
                (
                    mainCam.transform.position.x,
                    Player.transform.position.y,
                    mainCam.transform.position.z
                );

           mainCam.transform.position = Vector3.Lerp
                (
                    mainCam.transform.position,
                    targetPos,
                    0.5f
                );
        } //endif

        if (mainCam.transform.position.y > spawnY)
        {
            spawnY += Random.Range(3,4.5f);
            float spawnX = Random.Range(-5f,5f);
            SpawnTile(new Vector3(spawnX, spawnY));
        }
    }

    void SpawnTile(Vector3 pos)
    {
        pos.z = -0.99f;
        int RandomTile = Random.Range(0, Tiles.Length - 1);
        Instantiate(Tiles[RandomTile],pos,Tiles[RandomTile].transform.rotation);
    }
}


Unity Move Game Object With Finger swipe

Watch Video Tutorial On Youtube

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

public class Ball : MonoBehaviour
{
 private Rigidbody2D ball;
    public float BallSpeed;
    public float turnSpeen = 1;
 public float smoothTime;

    public GameObject Lose;
 Vector3 vel;
 // Use this for initialization
 void Start () {
        Lose.SetActive(false);

        ball = GetComponent<Rigidbody2D> ();
 }
 
 
 void FixedUpdate()
    {// 
        ball.velocity = new Vector2 (0, 1*BallSpeed);

  Vector3 pos =  Camera.main.ScreenToWorldPoint(Input.mousePosition);

        transform.position = Vector3.SmoothDamp (
                transform.position,
                new Vector3(pos.x, transform.position.y, transform.position.z),
                ref vel, 
                smoothTime
                );

  
 }

    void OnCollisionEnter2D(Collision2D col)
    {
        Lose.SetActive(true);
        print(col.gameObject.name);
    }
}

Monday, 16 October 2017

Unity 2D Infinite Background Scrolling Simplest Way

Watch Video Tutorial
Code for Infinite Background Scrolling IN Unity 2d game.

using UnityEngine;
using System.Collections;
 
public class Scroll : MonoBehaviour
{
 
    public float backGroundSize;
 
    private Transform cameraTransform;
    public Transform[] layers;
 
    private float viewZone = 0;
    private int leftIndex;
    private int rightIndex;
 
    private void Start()
    {
        cameraTransform = Camera.main.transform;   
 
        leftIndex = 0;
        rightIndex = layers.Length - 1;
    }
 
    void Update()
    {
        if (cameraTransform.position.< 
            (layers[leftIndex].transform.position.+ viewZone))
            ScrollLeft();
 
        if (cameraTransform.position.> 
            (layers[rightIndex].transform.position.- viewZone))
            ScrollRight();
    }
 
    private void ScrollLeft()
    {    
        layers[rightIndex].position = new 
           Vector3
           (1f * (layers[leftIndex].position.- backGroundSize), 0f0f);
        leftIndex = rightIndex;
        rightIndex--;
        if (rightIndex < 0)
            rightIndex = layers.Length - 1;
    }
    private void ScrollRight()
    {   
        layers[leftIndex].position = 
            new Vector3
            (1f * (layers[leftIndex].position.+ backGroundSize), 0f10f);
        rightIndex = leftIndex;
        leftIndex++;
        if (leftIndex == layers.Length)
            leftIndex = 0;
    }
}

Sunday, 15 October 2017

The Best Way to jump in Unity3d

Attach these two scripts with your player  and feel the difference in jumping it works both for 3D and 2D for 2D you have to make some adjustments in betterJump script.

WATCH VIDEO TUTORIAL

Player Script:


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

public class Player : MonoBehaviour {
    public float speed = 10.0F;
    public float rotationSpeed = 100.0F;
    // Use this for initialization
    void Start () {
  
 }
 
 // Update is called once per frame
 void Update () {
 
        float translation = Input.GetAxis("Vertical"* speed;
        float rotation = Input.GetAxis("Horizontal"* rotationSpeed;
 
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
 
        transform.Translate(00, translation);
        transform.Rotate(0, rotation, 0);
 
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jump();
        }
    }
    void jump()
    {
        transform.GetComponent<Rigidbody>().AddForce(new Vector3(0190 * Time.deltaTime, 0),ForceMode.Impulse);
    }
}



Better Jump Script (for 3D project)

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

public class fastJump : MonoBehaviour {
 
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;
    Rigidbody rb;
 
 
    void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        if (rb.velocity.< 0)
        {
            rb.velocity += Vector3.up * Physics.gravity.* (fallMultiplier - 1)
 * Time.deltaTime;
        }
    }
}

Better Jump Script (for 2D project)

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

public class fastJump : MonoBehaviour {
    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;
    Rigidbody2D rb;
    void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        if (rb.velocity.< 0)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.*
                (fallMultiplier - 1* Time.deltaTime;
        }
    }
 
 
}