精易论坛
标题:
飞机游戏之控制飞机飞行
[打印本页]
作者:
永久禁言
时间:
2013-2-18 14:09
标题:
飞机游戏之控制飞机飞行
本帖最后由 永久禁言 于 2013-2-18 14:23 编辑
using UnityEngine;
using System.Collections;
public class FPSFlight : MonoBehaviour
{
public float moveSpeed = 3.0f;
public float rotationSpeedX = 1;
public float rotationSpeedY = 1;
public float maxSpeed = 10.0f;
public Transform shipModel;
public float actualSpeed = 0.0f;
//因为要用场景中的camera来做位置限制
public Camera camera;
private Vector3 moveDirection = Vector3.zero;
private float baseRotationX = 0.0f;
private float baseRotationY = 0.0f;
private float oldSpeed = 0.0f;
private float recToX = 0f;
void Start()
{
}
void FixedUpdate ()
{
//控制机身旋转;
baseRotationX = Input.GetAxis("Horizontal")*rotationSpeedX;
baseRotationY = -Input.GetAxis("Vertical")*rotationSpeedY;
float angleZ = baseRotationX * 30;
float angleX = baseRotationY * 15;
shipModel.localEulerAngles = new Vector3 (angleX, 0, angleZ);
float toX = -Input.GetAxis("Horizontal") * moveSpeed;
float toZ = -Input.GetAxis("Vertical") * moveSpeed;
//限制;
Vector3 screenPos = camera.WorldToScreenPoint(shipModel.position);
if(Input.GetAxis("Horizontal")<0){
//向左移动;
if(screenPos.x<=50){
toX = 0;
}
}
if(Input.GetAxis("Horizontal")>0){
//向右移动;
if(screenPos.x>=Screen.width-50){
toX = 0;
}
}
if(Input.GetAxis("Vertical")<0){
//向下移动;
if(screenPos.y<=0){
toZ = 0;
}
}
if(Input.GetAxis("Vertical")>0){
//向上移动;
if(screenPos.y>=Screen.height-110){
toZ = 0;
}
}
//移动机身;
moveDirection.Set(toX,0, toZ);
this.transform.position += moveDirection * Time.deltaTime;
}
}
复制代码
作者:
小磊
时间:
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