Unity finger, mouse slide to achieve 360-degree rotation of the object, click the button to realize the rotation of the object

finger swipe program

First create an ObjectRotation script

/* 用于手指滑动屏幕让物体转动 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectRotation : MonoBehaviour
{
    public float rotateSpeed = 0.5f;  //设置默认初始转动速度,但是也可以在面板上直接更改数值

    private Vector2 lastTouchPosition;  //设定一个值用于保存最后位置

    void Update()
    {
        if (Input.touchCount == 1)   //判断是否有触屏动作
        {
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Moved)
            {
                Vector2 deltaPosition = touch.position - lastTouchPosition;   //
                float deltaX = deltaPosition.x / Screen.width * rotateSpeed;
                float deltaY = deltaPosition.y / Screen.height * rotateSpeed;
                transform.Rotate(Vector3.up, -deltaX, Space.Self);
                transform.Rotate(Vector3.right, deltaY, Space.Self);
            }

            lastTouchPosition = touch.position;
        }
    }
}

Then drag the script to the object that needs to be rotated

The mouse makes the object rotate 360

Because it is very troublesome to simulate the real machine every time, so I added a script that lets the mouse slide instead of the finger to replace the finger input. The
same is also to create a MouseRow script first.

/* 用于鼠标控制物体转动 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class MouseRow : MonoBehaviour
{
    private Rigidbody rigidbod;
    // Start is called before the first frame update
    void Start()
    {
        //隐藏或者显示物体
        //transform.gameObject.SetActive(true);
        rigidbod = this.GetComponent<Rigidbody>();  
		//此脚本挂在此刚体所属物体物体上,并获得刚体组件

    }
 
    // Update is called once per frame
    void Update()
    {
        rigidbod.constraints = RigidbodyConstraints.FreezeRotationZ;  
		//冻结刚体绕Z轴的旋转,物体只能绕X轴与Y轴旋转
        //如果鼠标左键按下
        if (Input.GetMouseButton(0))
        {
            float speed = 2.5f;//旋转跟随速度
            float OffsetX = Input.GetAxis("Mouse X");//获取鼠标x轴的偏移量
            float OffsetY = Input.GetAxis("Mouse Y");//获取鼠标y轴的偏移量
            transform.Rotate(new Vector3(OffsetY, -OffsetX, 0) * speed, Space.World);//旋转物体
			//获得空间角度
			/* float angleZ = transform.rotation.eulerAngles.z;
			float angleX = transform.rotation.eulerAngles.x;
			float angleY = transform.rotation.eulerAngles.y;
			Debug.Log("Rotation angleZ: " + angleZ);
			Debug.Log("Rotation angleX: " + angleX);
			Debug.Log("Rotation angleY: " + angleY); */
		}
    }
}

Click the button to rotate the object along a certain axis

Finally, I also need a certain stroke to rotate along the coordinates, the code is as follows:

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

public class RowPart : MonoBehaviour
{
    public int nowTurn;      //获得面板上初始化角度的判断
    void Start(){
    }
    //点击按钮后以Z轴旋转90度
    public void Click(){
        
        transform.Rotate(0, 0, 90,Space.Self);  //按照自身坐标的Z轴旋转90度(如果是要按照视界坐标则将pace.Self改成pace.world)
 
        nowTurn++;    //每点击一次增加一
        PlayerPrefs.GetInt("turn",nowTurn);     //运用PlayerPrefs.GetInt保存一个参数,该方法可以实现将值存储在内存中,方便之后读取
        PlayerPrefs.Save();                  //保存运用PlayerPrefs
        
        
        if(nowTurn == 4){                   //如果nowTurn等于4了归零
            nowTurn = 0;
        }
        
    }

}



There are my other parameters in it. If readers need to delete it, please delete it yourself. The last code has added PlayerPrefs knowledge, and I will write it elsewhere later.

public parameters in the script

The parameters of the public type in the script can be modified directly on the panel, I think this is really good
insert image description here

Guess you like

Origin blog.csdn.net/qq_19829077/article/details/130426360