Unity3D鼠标、键盘的操作

来源:互联网 发布:不一样的美男 知乎 编辑:程序博客网 时间:2024/06/06 01:57

键盘:

GetKey             当通过名称指定的按键被用户按住时返回true

GetKeyDown         当用户按下指定名称的按键时的那一帧返回true

GetKeyUp           在用户释放给定名字的按键的那一帧返回true

GetAxis(Horizontal")GetAxis(Verical用方向键或WASD键来模拟-11的平滑输入 


键盘判断:


If(Input.GetKeyDown(KeyCode.A)){//KeyCode表示包含键盘所有键

     print(“按下A键”);

}

If(Input.GetKeyUp(KeyCode.D)){//当按D键松开时

    print(“松开D键”);

}

If(Input.GetAxis(Horizontal")){//当按下水平键时

    print(“按下水平键”);

}

If(Input.GetKeyUp("Verical)){当按下垂直键时

    print(“按下垂直键”);

}


鼠标:


GetButton           根据按钮名称返回true当对应的虚拟按钮被按住时。 

GetButtonDown       在给定名称的虚拟按钮被按下的那一帧返回true

GetButtonUp         在用户释放指定名称的虚拟按钮时返回true


鼠标判断:


if(Input.GetButton("Fire1")){//Fire1表示按下鼠标左键

        print(“按下鼠标左键”);

}

if (Input.GetMouseButton(0)) {//0表示鼠标左键

     Debug.Log("按下鼠标左键");

}

 if (Input.GetMouseButton(1)) {//1表示鼠标右键

     Debug.Log("按下鼠标右键"); 

}

if (Input.GetMouseButton(2)) {//2表示鼠标中键

     Debug.Log("按下鼠标中键");

}


给物体施加普通力:


1、先给物体添加刚体

2transform.rigidbody.AddForce(0,0,1000); 

一个简单例子让小球撞破墙:


代码如下:

using UnityEngine;

using System.Collections;

public class Cube : MonoBehaviour {

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

if(Input.GetKey(KeyCode.W)){//当鼠标按下W键时,小球向前移动

transform.Translate(Vector3.forward);

}

if(Input.GetKey(KeyCode.S)){当鼠标按下S键时,小球向后移动

transform.Translate(Vector3.back);

}

if(Input.GetKey(KeyCode.A)){当鼠标按下A键时,小球向左移动

transform.Translate(Vector3.left);

}

if(Input.GetKey(KeyCode.D)){当鼠标按下D键时,小球向右移动

transform.Translate(Vector3.right);

}

if(Input.GetButton("Fire1")){//当点击鼠标左键时,小球撞塌墙

transform.rigidbody.AddForce(0,0,200);//物体向前移动的力为200

}

}

}


更多精彩请点击 http://www.gopedu.com/

0 0
原创粉丝点击