2019-12-21 15:38:04 zhunju0089 阅读数 90

需求:制作一个鼠标中键移动缩放和鼠标右键旋转的功能(鼠标右键旋转可以自行改成鼠标左键旋转)

效果图:

1.新建一个名为Target的立方体cube

2.新建一个材质球 Yellow颜色的并挂载到Target立方体上

 

3.新建一个脚本 MatrixAction.cs

4.MatrixAction.cs代码

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

public class MatrixAction : MonoBehaviour
{
    [Header("操作对象")]
    /// <summary>
    /// 操作对象
    /// </summary>
    public Transform m_target;
    void Start()
    {
        isDown1 = false;
        isDown2 = false;
    }
    /// <summary>
    /// 右键是否按下 
    /// </summary>
    private bool isDown1;

    /// <summary>
    /// 中键是否按下
    /// </summary>
    private bool isDown2;
    void Update()
    {
        // 按下了鼠标中键
        if (Input.GetMouseButtonDown(1))
            isDown1 = true;
        // 抬起了鼠标中键
        if (Input.GetMouseButtonUp(1))
            isDown1 = false;
        // 按下鼠标右键
        if (Input.GetMouseButtonDown(2))
            isDown2 = true;
        // 抬起了鼠标右键
        if (Input.GetMouseButtonUp(2))
            isDown2 = false;
        // 缩放 Input.mouseScrollDelta.y
        m_target.localScale += Time.deltaTime * m_target.localScale * Input.mouseScrollDelta.y;
    }
    private void LateUpdate()
    {
        if (isDown1)
        {
            // 计算单位时间内鼠标的偏移量
            float x = Input.GetAxis("Mouse X");
            float y = Input.GetAxis("Mouse Y");
            Vector3 rot = new Vector3(y, -x, 0);
            float spd = 10; //速度
            m_target.Rotate(spd * rot, Space.World);
        }
        if (isDown2)
        {
            // 计算单位时间内鼠标的偏移量
            float x = Input.GetAxis("Mouse X");
            float y = Input.GetAxis("Mouse Y");
            float spd = 0.4f;   //速度
            m_target.Translate(spd * new Vector3(x, y), Space.World);
        }
    }
}

5.新建一个Script空物体并将脚本MatrixAction.cs挂载上去,然后将Target拖拽到脚本上

6.为了Hierarchy层次更加清晰,新建几个空物体分隔各个组件 (可忽略)

2017-01-04 16:58:08 dickysun1987 阅读数 912
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mousedrag : MonoBehaviour
{

    private Vector3 targetScreenpos;//拖拽物体的屏幕坐标
    private Vector3 targetWorldpos;//拖拽物体的世界坐标
    private Transform target;//拖拽物体
    private Vector3 mouseScreenpos;//鼠标的屏幕坐标
    private Vector3 offset;//偏移量
    Color oricolor = Color.black;//物体本来的颜色

    void Start () 
    {
        target = transform;
    }

    void OnMouseEnter()
    {
        //当鼠标在物体上时,改变物体颜色。
        GetComponent<Renderer>().material.color = Color.blue;
    }
    void OnMouseExit()
    {
        GetComponent<Renderer>().material.color = oricolor;
    }

    //被移动物体需要添加collider组件,以响应OnMouseDown()函数
    //基本思路。当鼠标点击物体时(OnMouseDown(),函数体里面代码只执行一次),记录此时鼠标坐标和物体坐标,并求得差值。如果此后用户仍然按着鼠标左键,那么保持之前的差值不变即可。
    //由于物体坐标是世界坐标,鼠标坐标是屏幕坐标,需要进行转换。具体过程如下所示。
    IEnumerator OnMouseDown()
    {
        GetComponent<Renderer> ().material.color = Color.red;
        targetScreenpos = Camera.main.WorldToScreenPoint(target.position);  
        mouseScreenpos = new Vector3(Input.mousePosition.x,Input.mousePosition.y,targetScreenpos.z);
        offset = target.position -Camera.main.ScreenToWorldPoint(mouseScreenpos);

        while(Input.GetMouseButton(0))//鼠标左键被持续按下。
        {
            GetComponent<Renderer> ().material.color = Color.yellow;
            mouseScreenpos = new Vector3(Input.mousePosition.x,Input.mousePosition.y,targetScreenpos.z);
            targetWorldpos = Camera.main.ScreenToWorldPoint(mouseScreenpos) + offset;
            target.position = targetWorldpos;
            yield return new WaitForFixedUpdate();
        }
        GetComponent<Renderer>().material.color = Color.blue;
    }
} 
2017-01-04 17:00:34 dickysun1987 阅读数 2259
using UnityEngine;  
using System.Collections;  

public class CubeRotate : MonoBehaviour  
{  

    Vector3 StartPosition;  
    Vector3 previousPosition;  
    Vector3 offset;  
    Vector3 finalOffset;  
    Vector3 eulerAngle;  

    bool isSlide;  
    float angle;  

    void Start()  
    {  

    }  


    void Update()  
    {
        if (Input.GetMouseButtonDown(0))  
        {  
            StartPosition = Input.mousePosition;  
            previousPosition = Input.mousePosition;  
        }  
        if (Input.GetMouseButton(0))  
        {  
            offset = Input.mousePosition - previousPosition;  
            previousPosition = Input.mousePosition;  
            transform.Rotate(Vector3.Cross(offset, Vector3.forward).normalized, offset.magnitude, Space.World);  
        }  
        if (Input.GetMouseButtonUp(0))  
        {  
            finalOffset = Input.mousePosition - StartPosition;  
            isSlide = true;  
            angle = finalOffset.magnitude;  
        }  
        if (isSlide)  
        {  
            transform.Rotate(Vector3.Cross(finalOffset, Vector3.forward).normalized, angle * 2 * Time.deltaTime, Space.World);  
            if (angle > 0)  
            {  
                angle -= 5;  
            }  
            else  
            {  
                angle = 0;  
            }  
        }  
    }  
}  
2019-03-14 21:57:00 weixin_42094756 阅读数 181

unity3D鼠标和键盘获取

  1. 鼠标获取
    鼠标
  2. 键盘获取
    键盘输入的关键在于keycode关键字,详情可见Unity词典
    keycode键码
    在这里插入图片描述
  3. 键盘输入的另一种方式:
    Input.GetAxis(“Horizontal") //水平方向移动
    Input.GetAxis(“Vertical”) // 垂直方向移动 用方向键或WASD键来模拟-1到1的平滑输入

4.通过键盘获取移动的方法(常见的直上直下)

​1. float moveh = Input.GetAxis("Horizontal");
2. float movev = Input.GetAxis("Vertical");
3. transform.Translate(new Vector3(moveh, 0, movev) * Time.deltaTime * speed);​

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