Konumuza gelecek olursa konuyu daha iyi anlamak için Rotate fonksiyonundan bahsedeceğim.Bildiğiniz gibi rotate fonksiyonu bir objenin istediğimiz bir yönde dönmesini sağlıyor.Şöyle bir senaryo düşünelim.Bir küpümüz var bu küpü ok yönü ile sağa sola aşağı yukarı şekilde 90 derece döndürüyoruz.
Koda olarak göstermek gerekirse örneğin sağ yön tuşuna bastığımızda şu kodla döndereceğiz.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
transform.Rotate(-90, 0, 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
transform.Rotate(-90, 0, 0,Space.World) |
Direk kullanıldığımızda Rotate fonksiyonundaki sorunu yaşıyorum.Ama bunda direk Space.World ile dönmeyi world yapamıyoruz.Bunun yerine Quaternion.AngleAxis ile kendi rotatinumuzun çarpımı ile yapıyoruz.şimidi dönme için kodaları yapalım.
- İlk önce Quaternionları tanımlayalım.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Quaternion rotR = Quaternion.identity; | |
Quaternion eski; |
- Daha sonra update fonksiyonu içinde RotateTowards ile döndürmeyi ayarlayalım.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotR*eski, step*90); |
- En son olarakta yön tuşları ile AngleAxis ve konumları alalım.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Input.GetKeyDown(KeyCode.LeftArrow)) | |
{ | |
eski = transform.rotation; | |
rotR = Quaternion.AngleAxis(90, Vector3.right); | |
} | |
if (Input.GetKeyDown(KeyCode.RightArrow)) | |
{ | |
eski = transform.rotation; | |
rotR = Quaternion.AngleAxis(-90, Vector3.right); | |
} | |
if (Input.GetKeyDown(KeyCode.UpArrow)) | |
{ | |
eski = transform.rotation; | |
rotR = Quaternion.AngleAxis(-90, Vector3.forwart); | |
} | |
if (Input.GetKeyDown(KeyCode.DownArrow)) | |
{ | |
eski = transform.rotation; | |
rotR = Quaternion.AngleAxis(90, Vector3.forwart); | |
} |