Unity03之Input及飞机案例

using UnityEngine;
using System.Collections;
using System.Text;
public class Demo1 : MonoBehaviour {


    private GameObject obj;
    private StringBuilder strdialog=new StringBuilder ();
    // Use this for initialization
    void Start () {
    
        print (transform.up);
        print (transform.forward);
        print (transform.right);

        //实现正确的状态转换
//        this.transform.rotation = Quaternion.Euler (transform.eulerAngles+new Vector3 (0,30,-30));

    }
    
    // Update is called once per frame
    void Update () {
    

        MeshRenderer[] ms = this.GetComponentsInChildren<MeshRenderer> ();
        foreach(MeshRenderer m in ms){
            m.material.color = Color.red ;
        }

//        MyDestroy ();

        MyButton ();

    }

    public static  float MyDot(Vector3 a,Vector3 b){
        return a.x * b.x + a.y * b.y + a.z * b.z;
    }

    public static Vector3 MyCross(Vector3 a,Vector3 b){
        return new Vector3 (a.y * b.z - a.z * b.y, a.x * b.z - a.z * b.x, a.x * b.y - a.y * b.x);
    }

    public static Vector3 MyNormalize(Vector3 v){
    
        float num = Vector3.Magnitude (v);
        if(num>1E-05f){
            return v / num;
        }
        return Vector3.zero;
    }

    public void MyDestroy(){
    
        obj = GameObject.Find ("Cube/Cube1");

        if(Input.GetKey(KeyCode.A)){
            print (obj.name);
            Destroy (obj);
        }
    }

    public void MyButton(){
    
        //Fire1左键,Fire2右键,Fire3滚轮
//        if(Input.GetButtonDown("Fire3")){
//            Debug.Log ("down");
//        }
//        if(Input.GetButton("Fire3")){
//            Debug.Log ("click");
//        }
//        if(Input.GetButtonUp("Fire3")){
//            Debug.Log ("up");
//        }

        //0左键,1右键,2滚轮
//        if(Input.GetMouseButtonDown (0)){
//            Debug.Log ("0");
//        }
//        if(Input.GetMouseButton(0)){
//            Debug.Log ("00");
//        }
//        if(Input.GetMouseButtonUp(0)){
//            Debug.Log ("000");
//        }

//        if(Input.anyKey){
//            print ("按任意键继续anykey");
//        }
//        if(Input.anyKeyDown){
//            print ("anyKeyDown");
//        }

        //输入字符串
//        if(Input.inputString!=""){
//            strdialog.Append( Input.inputString);
//            print (strdialog.ToString());
//        }

//        if(Input.GetKey(KeyCode.A)){
//            print ("getkey:A键");
//        }
//        if(Input.GetKey("a")){
//            print ("getkey:A键");
//        }
//        if(Input.GetKeyDown(KeyCode.A)){
//            print ("Adown");
//        }
//        if(Input.GetKeyUp(KeyCode.A)){
//            print ("Aup");
//        }

        //平滑过度由0变为1(上,右)或-1(下,左)
        print (Input.GetAxis("Horizontal"));
        print (Input.GetAxis("Vertical"));

        //无平滑过程直接由0变为1(上,右)或-1(下,左)
//        print (Input.GetAxisRaw("Horizontal"));
//        print (Input.GetAxisRaw("Vertical"));
    }
}
/*
 * timeScale使Update和lateUpdate变快或变慢
 * 而fixedUpdate固定时间执行,由fixedTime影响

*/


飞机的旋转飞行代码:

public class PlaneDemo1 : MonoBehaviour {


    public float speed=5f;//螺旋桨旋转速度
    public float rotationY=1;//绕y旋转速度
    public float rotationZ=45;//绕z旋转速度
    private float ouLaZ;


    public GameObject propeller;//螺旋桨
    // Use this for initialization
    void Start () {
    
    //测试欧拉角的变化
//        ouLaZ = this.transform.eulerAngles.z;
//        print (ouLaZ);
//        this.transform.Rotate (0,0,-15);
//        ouLaZ = this.transform.eulerAngles.z;
//        print (ouLaZ);
//        this.transform.Rotate (0,0,5);
//        ouLaZ = this.transform.eulerAngles.z;
//        print (ouLaZ);

    }
    
    // Update is called once per frame
    void Update () {
    

        this.transform.Translate (Vector3.forward*speed*Time.deltaTime);
        if(propeller!=null){

            propeller.transform.Rotate (0,0,1000,Space.Self);
        }

        ouLaZ = this.transform.eulerAngles.z;

        print (ouLaZ);

        //涉及到网上拉升旋转,往下旋转,活动的角度在整个旋转的角度范围内
        if (Input.GetKey(KeyCode.A)) {

        
            if (ouLaZ <= 45 || ouLaZ >= 315) {//飞机绕z的活动范围,在此范围内,向上为正,即加z的旋转量,向下为负
                this.transform.Rotate (new Vector3 (0, 0, (rotationZ * Time.deltaTime)), Space.Self);
            }
            this.transform.Rotate (0, -rotationY, 0, Space.World);


        } else if (Input.GetKey(KeyCode.D)) {
            //飞机绕z的活动范围,在此范围内,向上为正,即加z的旋转量,向下则加负z的旋转量,当z的旋转量为0时欧拉角即为360
            //解析有点模糊
            if (ouLaZ <= 45 || ouLaZ >= 315) {
                this.transform.Rotate (0, 0, -1 * rotationZ * Time.deltaTime, Space.Self);
            }
            this.transform.Rotate (0, rotationY, 0, Space.World);
        } else {
        
            ReturnBalck ();
        }


    }

    void ReturnBalck(){
        if(ouLaZ<=180){
            if (ouLaZ - 0 <= 2) {
                this.transform.Rotate (0, 0, -1* rotationZ * Time.deltaTime* Time.deltaTime, Space.Self);
            } else {
            
                this.transform.Rotate (0,0,-1*rotationZ*Time.deltaTime,Space.Self);
            }
        }

        if(ouLaZ>180){
            if (360 - ouLaZ <= 2) {
                this.transform.Rotate (0, 0, 1 * rotationZ * Time.deltaTime* Time.deltaTime, Space.Self);
            } else {
            
                this.transform.Rotate (0,0,rotationZ*Time.deltaTime,Space.Self);
            }
        }
    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值