[英]Unity2D - How to rotate a 2D object on touch/click/press
First of all, I'll let you know that I'm new to Unity and to coding overall (I do know some very basic javascript coding). 首先,我会让您知道我是Unity和整体编码的新手(我确实知道一些非常基本的javascript编码)。 My question: How can I rotate a 2D object (prefab) 120 degrees on a certain axis (in my case the z-axis, so it rotates like you're looking at a steering wheel) every time I touch on the screen.
我的问题:每次我在屏幕上触摸时,如何在某个轴(例如z轴,所以它就像您在操纵方向盘一样)上旋转2D对象(预制)120度。 Right now I have it like this:
现在我有这样的:
function TouchOnScreen ()
{
if (Input.touchCount > 0)
{
var touch = Input.touches[0];
if (touch.position.x < Screen.width/2)
{
transform.rotation = Quaternion.Euler(0,0,120);
Debug.Log("RotateRight");
}
else if (touch.position.x > Screen.width/2)
{
transform.rotation = Quaternion.Euler(0,0,-120);
Debug.Log("RotateLeft");
}
}
}
This code rotates the object whenever I press on a certain side of the screen, but not how I want it to. 每当我按屏幕的某一侧时,此代码都会旋转对象,但不会按我希望的方式旋转。 I want it to rotate so you see the object rotating from A to B, but not (like it is now) in one frame from A to B. Also, this code lets me only rotate one time to each direction.
我希望它旋转,以便您看到对象从A旋转到B,但不(像现在一样)在从A到B的一帧中旋转。而且,此代码只允许我向每个方向旋转一次。
How can I make it that whenever I press on a certain side of the screen, that it adds or subtracts to/from the previous rotated angle, so I can keep on rotating. 如何确保每当我按下屏幕的某一侧时,它就会与上一个旋转角度相加或相减,这样我就可以继续旋转。
NOTE: Please use javascript, and if you know a simpler code, let me know! 注意:请使用javascript,如果您知道更简单的代码,请告诉我!
Help is highly appreciated, thanks in advance! 非常感谢您的帮助,在此先感谢您!
Instead of 代替
transform.rotation = Quaternion.Euler(0,0,-120);
You use: 你用:
var lSpeed = 10.0f; // Set this to a value you like
transform.rotation = Quaterion.Lerp ( transform.rotation, Quaternion.Euler(0,0,-120), Time.deltaTime*lSpeed);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.