游戏蛮牛学习群(纯技术交流,不闲聊):159852603
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   碎碎

物体根据鼠标移动而旋转

个人分类: 鼠标控制 | 2018-7-11 15:55
0 个评论 | 阅读 122 | 收藏 | 举报
第一部分:物体根据鼠标移动而旋转-----PC端(亲测)
PC端鼠标拖动而转动参考链接:http://blog.csdn.net/liujunjie612/article/details/54136337

[code]csharpcode:

/*
 * 鼠标控制物体360旋转
 * 创建日期:2018.7.11
 * 最近修改:
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseRotate : MonoBehaviour {
    public Transform obj;
    //Rotation speed
    public float speed =5;
    //Judge whether the mouse is pressed or not
    private bool _mouseDown = false;
    void Update()
    {
        //The left button of the mouse is pressed
        if(Input.GetMouseButtonDown(0))
            _mouseDown = true;
        //The left button of the mouse is raised
        else if(Input.GetMouseButtonUp(0))
            _mouseDown = false;

        if(_mouseDown)
        {
            //The offset of the left and right slipping of the mouse
            float fMouseX = Input.GetAxis("Mouse X");
            //The offset of the upper and lower slipping of the mouse
            float fMouseY = Input.GetAxis("Mouse Y");
            //Left and right rotation
            obj.Rotate(Vector3.up, -fMouseX * speed, Space.World);
            //Spin up and down
            obj.Rotate(Vector3.right, fMouseY * speed, Space.World);
        }
    }
}
第二部分:物体根据鼠标移动而旋转-----手机端

[code]csharpcode:

    private Vector3 startFingerPos;
    private Vector3 nowFingerPos;
    private float xMoveDistance;
    private float yMoveDistance;
    private int backValue = 0;
    public GameObject obj;
    void Update()
    {
        if (Input.touchCount <= 0)
        {
            return;
        }
 
        if (Input.GetTouch(0).phase == TouchPhase.Began)
        {
            //Debug.Log("======开始触摸=====");  
            startFingerPos = Input.GetTouch(0).position;
        }
 
        nowFingerPos = Input.GetTouch(0).position;
 
        if ((Input.GetTouch(0).phase == TouchPhase.Stationary) || (Input.GetTouch(0).phase == TouchPhase.Ended))
        {
            startFingerPos = nowFingerPos;
            //Debug.Log("======释放触摸=====");  
            return;
        }
        //          if (Input.GetTouch(0).phase == TouchPhase.Ended) {  
        //                
        //          }  
        if (startFingerPos == nowFingerPos)
        {
            return;
        }
        xMoveDistance = Mathf.Abs(nowFingerPos.x - startFingerPos.x);
        yMoveDistance = Mathf.Abs(nowFingerPos.y - startFingerPos.y);
 
        if (xMoveDistance > yMoveDistance)
        {
            if (nowFingerPos.x - startFingerPos.x > 0)
            {
                //Debug.Log("=======沿着X轴负方向移动=====");  
                backValue = -1; //沿着X轴负方向移动  
            }
            else
            {
                //Debug.Log("=======沿着X轴正方向移动=====");  
                backValue = 1; //沿着X轴正方向移动  
            }
        }
        else
        {
            if (nowFingerPos.y - startFingerPos.y > 0)
            {
                //Debug.Log("=======沿着Y轴正方向移动=====");  
                backValue = 2; //沿着Y轴正方向移动  
            }
            else
            {
                //Debug.Log("=======沿着Y轴负方向移动=====");  
                backValue = -2; //沿着Y轴负方向移动  
            }
 
        }
        if (backValue == -1)
        {
            obj.transform.Rotate(Vector3.up * -1 * Time.deltaTime * 50, Space.World);
        }
        else if (backValue == 1)
        {
            obj.transform.Rotate(Vector3.up * Time.deltaTime * 50, Space.World);
        }
        else if (backValue == 2)
        {
            obj.transform.Rotate(Vector3.right * Time.deltaTime * 50, Space.World);
        }
        else if (backValue == -2)
        {
            obj.transform.Rotate(Vector3.right * -1 * Time.deltaTime * 50, Space.World);
        }  
    }  

0 0

作者的其他最新博客

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

返回顶部