unity3d 鼠标控制物体上下、左右、旋转

using UnityEngine;
using System.Collections;


public class Rotate : MonoBehaviour {
   
    public GameObject cube;  //要拖拽的物体
    Vector3 mouse;    //鼠标
    Vector3 screeenV;  //存储cube的屏幕坐标
    Vector3 world;    //记录鼠标坐标转成的世界坐标
    void Update()
    {
        screeenV = Camera.main.WorldToScreenPoint(cube.transform.position);
        //当鼠标第一次单击时记录下cube在场景中的坐标,并把世界坐标转成屏幕坐标
        mouse = Input.mousePosition;  //当鼠标移动时记录下鼠标的坐标
        mouse.z = screeenV.z;  //因为鼠标的z坐标为0,所以需要一个z坐标
                                 //把鼠标的屏幕坐标转换成世界坐标
        world = Camera.main.ScreenToWorldPoint(mouse);
        //当鼠标移动时,cube也发生移动,为了让cube的y轴不发生移动,设y轴为原来的y轴
        if (Input.GetMouseButton(0))
        {
            cube.transform.position = new Vector3(world.x, cube.transform.position.y, world.z);
            print(cube.transform.position); 
        }
        if (Input.GetMouseButton(1))
        {
            cube.transform.position = new Vector3(world.x, world.y, cube.transform.position.z);
            print(cube.transform.position); 
        }
        if (Input.GetMouseButton(2))
        {
            cube.gameObject.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") *
                Time.deltaTime * 300, -Input.GetAxis("Mouse X") * Time.deltaTime * 300, 0));
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值