Unity学习笔记(四)之模型移动

一、Transform基本移动函数

1. 按照指定方向移动一定距离,应用范围:wasd键以及上下左右控制类似
float TranslateSpeed = 10f;
//Vector3.forward 表示“向前”
transform.Translate(Vector3.forward *TranslateSpeed);
2. 全方向移动一定的距离
//x轴移动速度移动速度 
float xSpeed = -5f;

//z轴移动速度移动速度 
float  zSpeed = 10f;

//向x轴移动xSpeed,同时想z轴移动zSpeed,y轴不动 
transform.Translate(xSpeed,0,zSpeed);
3. 重置坐标,移动到世界坐标的相对位置,也就是修改对应position属性
//x轴坐标 
float xPostion = -5f;
//z轴坐标 
float zPostion = 10f;
//直接将当前物体移动到x轴为xPostion,y轴为0,z轴为zPostion的三维空间位置。
transform.position = new Vector3(xPostion,0,zPostion);

二、输入控制

1. 键盘控制

GetKey 当通过名称指定的按键被用户按住时返回true
GetKeyDown 当用户按下指定名称的按键时的那一帧返回true。
GetKeyUp 在用户释放给定名字的按键的那一帧返回true。
GetAxis(“Horizontal")GetAxis(“Verical”) 用方向键或WASD键来模拟-1到1的平滑输入
键盘判断:

    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 ("按下垂直键");
    }
  • 持续按,持续输出
//按下键盘“上方向键”
if(Input.GetKey ("up"))
  print("Up!");

//按下键盘“W键”
if(Input.GetKey(KeyCode.W);)
  print("W!");
  • 一次检测(在键盘按下去的时候)
if(Input.GetKeyDown(KeyCode.Q))
{
   print("Q");
}
  • GetAxisC#键盘控制脚本
using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public float speed = 10.0F;
    public float rotationSpeed = 100.0F;
    void Update() {
        float translation = Input.GetAxis("Vertical") * speed;
        float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
        transform.Translate(0, 0, translation);
        transform.Rotate(0, rotation, 0);
    }
}
2. 鼠标控制

GetButton 根据按钮名称返回true当对应的虚拟按钮被按住时。
GetButtonDown 在给定名称的虚拟按钮被按下的那一帧返回true。
GetButtonUp 在用户释放指定名称的虚拟按钮时返回true。

if (Input.GetButton ("Fire1")) {
    //Fire1表示按下鼠标左键
    print("按下鼠标左键");
}
if (Input.GetMouseButton(0)) {
    //Fire1表示按下鼠标左键
    print("按下鼠标左键");
}
if (Input.GetMouseButton(1)) {
    //1表示按下鼠标右键
    print("按下鼠标右键");
}
if (Input.GetMouseButton(2)) {
    //2表示按下鼠标中键
    print("按下鼠标中键");
}
  • GetAxisC#鼠标控制脚本
using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public float horizontalSpeed = 2.0F;
    public float verticalSpeed = 2.0F;
    void Update() {
        float h = horizontalSpeed * Input.GetAxis("Mouse X");
        float v = verticalSpeed * Input.GetAxis("Mouse Y");
        transform.Rotate(v, h, 0);
    }
}
按住鼠标拖动物体旋转和自定义角度旋转物体脚本
float speed = 100.0f;
float x;
float z;

void Update () {
  if(Input.GetMouseButton(0)){//鼠标按着左键移动 
    y = Input.GetAxis("Mouse X") * Time.deltaTime * speed;               
    x = Input.GetAxis("Mouse Y") * Time.deltaTime * speed; 
  }else{
    x = y = 0 ;
  }
  
  //旋转角度(增加)
  transform.Rotate(new Vector3(x,y,0));
  /**---------------其它旋转方式----------------**/
  //transform.Rotate(Vector3.up *Time.deltaTime * speed);//绕Y轴 旋转 

  //用于平滑旋转至自定义目标 
  pinghuaxuanzhuan();
}


//平滑旋转至自定义角度 

void OnGUI(){
  if(GUI.Button(Rect(Screen.width - 110,10,100,50),"set Rotation")){
    //自定义角度

    targetRotation = Quaternion.Euler(45.0f,45.0f,45.0f);
    // 直接设置旋转角度 
    //transform.rotation = targetRotation;

    // 平滑旋转至目标角度 
    iszhuan = true;
  }
}

bool iszhuan= false;
Quaternion targetRotation;

void pinghuaxuanzhuan(){
  if(iszhuan){
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 3);
  }
}
键盘控制物体缩放脚本
float speed = 5.0f;
float x;
float z;

void Update () {
    x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;    //水平           
    z = Input.GetAxis("Vertical") * Time.deltaTime * speed;      //垂直//"Fire1","Fine2","Fine3"映射到Ctrl,Alt,Cmd键和鼠标的三键或腰杆按钮。新的输入轴可以在Input Manager中添加。
    transform.localScale += new Vector3(x, 0, z);  
    
    /**---------------重新设置角度(一步到位)----------------**/
    //transform.localScale = new Vector3(x, 0, z);
}

其他资料参考:Unity3D研究院之脚本实现模型的平移与旋转
使用脚本控制模型移动旋转与碰撞

你可能感兴趣的:(Unity学习笔记(四)之模型移动)