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.

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读