精易论坛

标题: 飞机游戏之控制飞机飞行 [打印本页]

作者: 永久禁言    时间: 2013-2-18 14:09
标题: 飞机游戏之控制飞机飞行
本帖最后由 永久禁言 于 2013-2-18 14:23 编辑
  1. using UnityEngine;
  2. using System.Collections;

  3. public class FPSFlight : MonoBehaviour
  4. {

  5.         public float moveSpeed = 3.0f;
  6.         public float rotationSpeedX = 1;
  7.         public float rotationSpeedY = 1;
  8.         public float maxSpeed = 10.0f;
  9.         public Transform shipModel;
  10.         public float actualSpeed = 0.0f;
  11.         //因为要用场景中的camera来做位置限制
  12.         public Camera camera;

  13.         private Vector3 moveDirection = Vector3.zero;
  14.         private float baseRotationX = 0.0f;
  15.         private float baseRotationY = 0.0f;
  16.         private float oldSpeed = 0.0f;
  17.        
  18.         private float recToX = 0f;
  19.         void Start()
  20.         {
  21.                
  22.         }
  23.        
  24.         void FixedUpdate ()
  25.         {
  26.                 //控制机身旋转;
  27.                 baseRotationX = Input.GetAxis("Horizontal")*rotationSpeedX;
  28.                 baseRotationY = -Input.GetAxis("Vertical")*rotationSpeedY;
  29.                 float angleZ = baseRotationX * 30;
  30.                 float angleX = baseRotationY * 15;
  31.                 shipModel.localEulerAngles = new Vector3 (angleX, 0, angleZ);
  32.                
  33.                 float toX = -Input.GetAxis("Horizontal") * moveSpeed;
  34.                 float toZ = -Input.GetAxis("Vertical") * moveSpeed;
  35.                
  36.                 //限制;
  37.                 Vector3 screenPos = camera.WorldToScreenPoint(shipModel.position);
  38.                 if(Input.GetAxis("Horizontal")<0){
  39.                         //向左移动;
  40.                         if(screenPos.x<=50){
  41.                                 toX = 0;
  42.                         }
  43.                 }
  44.                
  45.                 if(Input.GetAxis("Horizontal")>0){
  46.                         //向右移动;
  47.                         if(screenPos.x>=Screen.width-50){
  48.                                 toX = 0;
  49.                         }
  50.                 }
  51.                
  52.                 if(Input.GetAxis("Vertical")<0){
  53.                         //向下移动;
  54.                         if(screenPos.y<=0){
  55.                                 toZ = 0;
  56.                         }
  57.                 }
  58.                
  59.                 if(Input.GetAxis("Vertical")>0){
  60.                         //向上移动;
  61.                         if(screenPos.y>=Screen.height-110){
  62.                                 toZ = 0;
  63.                         }
  64.                 }
  65.                
  66.                 //移动机身;
  67.                 moveDirection.Set(toX,0, toZ);
  68.                 this.transform.position += moveDirection * Time.deltaTime;
  69.         }
  70.        
  71. }
复制代码

作者: 小磊    时间: 2013-2-18 14:11
换行都没,坑爹了
作者: 永久禁言    时间: 2013-2-18 14:23
小磊 发表于 2013-2-18 14:11
换行都没,坑爹了

我编辑下。。。。。。。。。
作者: 大姨夫    时间: 2013-2-18 14:27
俺不懂,顶下




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4