增加了控制跑步時間的移動指令碼

來源:互聯網
上載者:User

標籤:oat   field   stat   mys   run   empty   runtime   oca   public   

using UnityEngine;
using System.Collections;

public class MySoliderMove : MonoBehaviour
{

Transform _head;

Transform _gun;

public float workTime;
float _runTime=10;
bool isEmpty;
// Use this for initialization
void Start()
{
_head = transform.FindChild("Head");
_gun = transform.FindChild("M16");


}

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

// Cursor.lockState = CursorLockMode.Locked;
_gun.forward = _head.forward; //讓槍指向相機的中間位置
Move();
Rotate();
Run();

Jump();
}

[SerializeField]
float moveSpeed = 10f;
void Move()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");


Vector3 desPos = Vector3.right * horizontal + Vector3.forward * vertical;
transform.Translate(desPos * Time.deltaTime * moveSpeed);

}

void Run()
{
if (Input.GetKey(KeyCode.LeftShift)&&!isEmpty) //能量槽不空才能進入迴圈
{
workTime += Time.deltaTime; //跑步時間
moveSpeed = 30f; //加速

if (workTime>_runTime) //如果跑步的時間超過了上限
{
isEmpty = true; //設定為空白
}
}
else
{
if (workTime>0)
{
workTime -= Time.deltaTime;
}
else
{
workTime = 0;
isEmpty = false;
}
moveSpeed = 10f; //其他情況下的速度都為正常值
}
}

void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
transform.Translate(Vector3.up*1.9f);
}
}

float rotationY = 0f;
[SerializeField]
float rotateSpeed = 20f;
void Rotate()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");

transform.Rotate(mouseX * Vector3.up * rotateSpeed);

rotationY += mouseY;

rotationY = Mathf.Clamp(rotationY, -60, 60); //控制人物視野

_head.localEulerAngles = Vector3.left * rotationY;


//public class FirstView : MonoBehaviour
//{

// 方向靈敏度
// public float sensitivityX = 10F;
// public float sensitivityY = 10F;

// 上下最大視角(Y視角)
// public float minimumY = -60F;
// public float maximumY = 60F;

// float rotationY = 0F;

// void Update()
// {
// 根據滑鼠移動的快慢(增量), 獲得相機左右旋轉的角度(處理X)
// float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

// 根據滑鼠移動的快慢(增量), 獲得相機上下旋轉的角度(處理Y)
// rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
// 角度限制. rotationY小於min,返回min. 大於max,返回max. 否則返回value
// rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

// 總體設定一下相機角度
// transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
// }

// void Start()
// {
// Make the rigid body not change rotation
// if (rigidbody)
// rigidbody.freezeRotation = true;
// }

}

}

增加了控制跑步時間的移動指令碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.