Unity Vector3 class

Unity 学习笔记汇总
Random官方API使用文档

1. Rotating cube

1.1. Foreground

在这里插入图片描述

1.2. Code

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

public class test : MonoBehaviour
{
    Vector3 axis;
    float speed;

    public GameObject cube;
    float mouseX, mouseY;

    Vector3 currentMouse;

    float drag = 0.2f;
    float scaleFactor = 7;



    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        /*
         Description
            Returns whether the given mouse button is held down.

            button values are 0 for left button, 1 for right button, 2 for the middle button. The return is true when the mouse button is pressed down, and false when released.
         */
        {
            mouseX = Input.GetAxis("Mouse X");
            mouseY = Input.GetAxis("Mouse Y");

            currentMouse = new Vector3(mouseX, mouseY, 0);
            axis = Vector3.Cross(currentMouse, Vector3.forward);
            speed = Vector3.Magnitude(currentMouse) * scaleFactor;
        }
        else
        {
            speed = Mathf.Clamp(speed - drag, 0, Mathf.Infinity);
        }
        cube.transform.Rotate(axis, speed, Space.World);
    }
}

1.3. Result

When the left mouse button is pressed, the cube rotates as the mouse moves.

发布了605 篇原创文章 · 获赞 637 · 访问量 140万+

猜你喜欢

转载自blog.csdn.net/COCO56/article/details/105012943