26 Temmuz 2016 Salı

UNITY GETCOMPONENT KULLANIMI

Bir nesnenin componentlerine ulaşmak için kullanılır.Bu nesne dışarıdan bir nesne olabileceği gibi scriptin yazılmış olduğu nesne içinde kullanılabilir.Hatta bu komutun bazı varyasyonları ile kendi alt nesnelerinin componentlerine de ulaşabilirler.

public class UsingOtherComponents : MonoBehaviour
{
    public GameObject otherGameObject;
    
    
    private AnotherScript anotherScript;
    private YetAnotherScript yetAnotherScript;
    private BoxCollider boxCol;
    
    
    void Awake ()
    {
        anotherScript = GetComponent<AnotherScript>();
        yetAnotherScript = otherGameObject.GetComponent<YetAnotherScript>();
        boxCol = otherGameObject.GetComponent<BoxCollider>();
    }
    
    
    void Start ()
    {
        boxCol.size = new Vector3(3,3,3);
        Debug.Log("The player's score is " + anotherScript.playerScore);
        Debug.Log("The player has died " + yetAnotherScript.numberOfPlayerDeaths + " times");
    }
}

public class AnotherScript : MonoBehaviour
{
    public int playerScore = 9001;
}
public class YetAnotherScript : MonoBehaviour
{
    public int numberOfPlayerDeaths = 3;
}

UNITY ONMOUSEDOWN KOMUTU

void OnMouseDown ()
    {
        rigidbody.AddForce(-transform.forward * 500f);
        rigidbody.useGravity = true;
    }
Bu komut ile nesnenin üzerine mouse ile tıklanıldığında ne yapılacağını göstermiş olursunuz.Mouse ile tıklanıldığında sadece 1 kez çalışır.Basılı tutulması gibi durumlar için veya mouse üzerine gelince bırakınca gibi kodlar için IPointer interfaceleri tanımlanmalıdır.Bunlar ise eventSystem içindedir.Genelde canvas ve UI elemanlar için kullanırım.Lakin onMouseDown komutu oyun için de kullanılır. 

22 Temmuz 2016 Cuma

GETBUTTON VE GETKEY

Bu kavramlar kullanıcının klavye etkileşimini ifade eder.Şimdi daha akından bakalım

- GetButton = Belirlenen bir tuşa basılı kaldığı sürece true değer döner.

- GetButtonDown = Belirlenen bir tuşa ilk basıldığında 1 kez true değer döner.

- GetButtonUp = Belirlenen ve basılan tuş bırakıldıında 1 seferlik true değer döner

GetKey bundan farklı değildir sadece getButton da Key ile ulaşılan bir yerin ismini direkt olarak kullanırsınız. Örneğin "jump" derseniz space olduğunu anlar ama getkey de space e basılacağını söylemelisiniz.  .

GetKey Kullanımı

  void Update ()
    {
        bool down = Input.GetButtonDown("Jump");
        bool held = Input.GetButton("Jump");
        bool up = Input.GetButtonUp("Jump");
        
        if(down)
        {
            graphic.texture = downgfx;
        }
        else if(held)
        {
            graphic.texture = heldgfx;
        }
        else if(up)
        {
            graphic.texture = upgfx;
        }
        else
        {
            graphic.texture = standard;
        }
    
        guiText.text = " " + down + "\n " + held + "\n " + up;
    }

GetButton Kullanımı

 void Update ()
    {
        bool down = Input.GetKeyDown(KeyCode.Space);
        bool held = Input.GetKey(KeyCode.Space);
        bool up = Input.GetKeyUp(KeyCode.Space);
        
        if(down)
        {
            graphic.texture = downgfx;
        }
        else if(held)
        {
            graphic.texture = heldgfx;
        }
        else if(up)
        {
            graphic.texture = upgfx;
        }
        else
        {
            graphic.texture = standard; 
        }
        
        guiText.text = " " + down + "\n " + held + "\n " + up;
    }

UNITY DESTROY

Destroy oyun objelerini oyundan kaldırmaya yarar. Hatta sadece oyun objelerini değil obje componentlerinide kaldırmaya yarar.

if(Input.GetKey(KeyCode.Space))
        {
            Destroy(gameObject);
        }
Burada scriptin bağlı oldugu objeyi kaldırma işlemi gerçekleştiriyoruz.

if(Input.GetKey(KeyCode.Space))
        {
            Destroy(other);
        }
Burada ise dışarıdan gelen other isimli bir nesneyi space tuşuna basınca yoketmeyi sağlıyoruz.

if(Input.GetKey(KeyCode.Space))
        {
            Destroy(GetComponent<MeshRenderer>());
        }
Hatta objenin görünürlüğünü de silebiliyoruz.

21 Temmuz 2016 Perşembe

LookAt FONKSİYONU

Bu fonksiyon nesnenin belirlenen koordinatlara dönmesini sağlar. Örneğin oyundaki bir kameraya aşağıdaki kod verirsek

using UnityEngine;
using System.Collections;

public class CameraLookAt : MonoBehaviour
{
    public Transform target;
    
    void Update ()
    {
        transform.LookAt(target);
    }
}
Kameranın yeri değişmez lakin sürekli o cisme doğru bakar. Sadece kamera değil her objede bullanılabilir. Örneğin üzerine doğru gelen bir zombie için :) 

TRANSLATE VE ROTATE



 void Update ()
    {
        if(Input.GetKey(KeyCode.UpArrow))
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
        
        if(Input.GetKey(KeyCode.DownArrow))
            transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
        
        if(Input.GetKey(KeyCode.LeftArrow))
            transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
        
        if(Input.GetKey(KeyCode.RightArrow))
            transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
    }
Bu kod ile Translate ve Rotate fonksiyonunu görmekteyiz. Translate bir nesnenin hareketini sağlar. Eski konumunun üzerine ekleme usülü ile çalışır. Rotate ise bir nesneyi belirletiğimiz yönde döndürmeye yarar aynı translate mantığıyla çalışır.