Unity3D自学笔记——脚本开发的输入与控制

键盘的输入与控制

public static bool GetButton(string buttonName);
当InputManager设置的buttonName键被按下返回true,若一直按着按键不放,则一直返回true。在项目中尽量使用GetButton,以免后期代码过多,导致按键混乱造成不必要的麻烦。

Input.GetButton("Fire1")

public static bool GetButtonDown(string buttonName);
当InputManager设置的buttonName键被按下瞬间返回true,在整过按键操作中只会返回一次true。

public static bool GetButtonUp(string buttonName);
当InputManager设置的buttonName键被释放返回true。

 bool key;
    bool keyDown;
    bool keyUp;
    // Update is called once per frame
    void Update () {
        key = Input.GetButton("Jump");
        keyDown = Input.GetButtonDown("Jump");
        keyUp = Input.GetButtonUp("Jump");

        if (keyDown)
        {
            this.transform.localScale = transform.localScale + Vector3.one;
        }
        if (key)
        {
            this.transform.Rotate(Vector3.up, 45 * Time.deltaTime);
        }
        if(keyUp)
        {
            this.transform.localScale = transform.localScale - Vector3.one;
        }
    }

public static bool GetKey(string name);
当键盘的name按键被按下返回true。

Input.GetKey("up")

public static bool GetKey(KeyCode key);
根据KeyCode获得键盘name

Input.GetKey(KeyCode.UpArrow)

public static float GetAxis(string axisName);
根据坐标轴名称返回虚拟坐标系中的值。返回值在-1 到 1之间,轴名称为InputManger里每个轴的Name属性,如Horizontal, Vertical。Gravity代表从-1或1回到0的速度,Sensitivity代表从0到-1或1的速度,默认都为3,值越大速度越快。Snap若被勾选,则按下相方向键,立刻归0,若为勾选,则根据Gravity速度返回至0。如角色控制一般都勾选Snap,赛车游戏则根据情况勾选。

 bool key;
    bool keyDown;
    float axis;
    // Update is called once per frame
    void Update () {
        axis = Input.GetAxis("Mouse X");
        this.transform.position = new Vector3(axis, 0, 0);
    }

public static float GetAxisRaw(string axisName);
根据坐标轴名称返回虚拟坐标系中的值。键盘和控制器取值范围在-1…1之间,此时输入没有使用平滑。键盘输入必然会是-1、0或1。

鼠标的输入与控制

MonoBehaviro
参见 MonoBehaviro Input 输入

光线投射
public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

origin : 射线起始点
direction:射线方向
hitInfo:射到物体上的信息
maxDistance : 射线最大长度
layerMask:物体层级遮罩,不设置则打到所有物体
这里写图片描述

例1:物体撞击地面前减速

public float rayDistance = 4f;

    void Update()
    {
        Ray testRay = new Ray(transform.position, Vector3.down);
        RaycastHit hitInfomation;

        //射线撞击到GUI元素或Collider
        if (Physics.Raycast(testRay,out hitInfomation, rayDistance))
        {
            //撞击到地面
            if (hitInfomation.collider.name == "Plane")
            {
                //减速
                transform.GetComponent<Rigidbody>().drag = 8f;
            }
        }

        Debug.DrawRay(transform.position, Vector3.down * rayDistance, Color.red);
    }

例2:利用RayCast监测鼠标点击

    public Camera myCamera = null;

    void Update()
    {
        if (!Input.GetMouseButtonDown(0))
            return;
        //镜头的点转换为射线
        Ray r = myCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if(Physics.Raycast(r, out hit, 1000))
        {
            if(hit.collider.name == "Cube")
            {
                Debug.Log("Mouse Click");
            }
        }
    }

Space 空间类型

World Space:世界坐标系的位置
Screen Space:屏幕空间,2维空间,Z轴为0
Viewport Space:2维空间,(0,0) 到 (1,1)

Camera.ScreenPointToRay
返回一条从镜头到Screen的射线
Camera.ViewportPointToRay
返回一条从镜头到Viewport的射线

例3:利用鼠标拖拽移动物体

 public Camera myCamera = null;
    public float depth = 10f;

    void OnMouseEnter()
    {
        transform.localScale = transform.localScale + Vector3.one;
    }

    void OnMouseExit()
    {
        transform.localScale = transform.localScale - Vector3.one;
    }

    void OnMouseOver()
    {
        transform.Rotate(Vector3.up, 45 * Time.deltaTime);
    }

    void OnMouseDrag()
    {
        //MoveObject();
        MoveObjectFixdepth();
    }

    void MoveObject()
    {
        Ray r = myCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if(Physics.Raycast(r, out hit, 1000f, 1))
        {
            this.transform.position = new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z);
            Debug.DrawLine(r.origin, hit.point, Color.red);
        }
    }

    void MoveObjectFixdepth()
    {
        Vector3 mouseScreen = Input.mousePosition;
        mouseScreen.z = depth;
        Vector3 mouseWorld = myCamera.ScreenToWorldPoint(mouseScreen);
        this.transform.position = mouseWorld;
    }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值