You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.8 KiB
89 lines
2.8 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using HTC.UnityPlugin.Vive; |
|
using UnityEngine.SceneManagement; |
|
|
|
//用于实现玩家在场景中自由走动的脚本 |
|
public class MovingCtrol : MonoBehaviour { |
|
public GameObject player; |
|
public GameObject camera_; |
|
public float walkSpeed; |
|
public float runSpeed; |
|
public float hight; |
|
public AudioSource walkVoice; |
|
public AudioSource runVoice; |
|
|
|
// Use this for initialization |
|
void Start () { |
|
if (walkSpeed <= 0) { |
|
walkSpeed = 5f; |
|
} |
|
if (runSpeed<=0) { |
|
runSpeed = 10f; |
|
} |
|
} |
|
|
|
// Update is called once per frame |
|
void Update () { |
|
//玩家的碰撞盒位置重置 |
|
player.GetComponent<CapsuleCollider>().center = new Vector3(camera_.transform.localPosition.x, hight, camera_.transform.localPosition.z); |
|
if (ViveInput.GetPress(HandRole.RightHand, ControllerButton.PadTouch)) |
|
{ |
|
Walk(); |
|
} |
|
if (ViveInput.GetPress(HandRole.RightHand, ControllerButton.Pad)) { |
|
Run(); |
|
} |
|
//声音相关 |
|
if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.PadTouch)) |
|
{ |
|
runVoice.Stop(); |
|
walkVoice.Play(); |
|
} |
|
if (ViveInput.GetPressDown(HandRole.RightHand, ControllerButton.Pad)) |
|
{ |
|
walkVoice.Stop(); |
|
runVoice.Play(); |
|
} |
|
if (ViveInput.GetPressUp(HandRole.RightHand, ControllerButton.PadTouch)) |
|
{ |
|
walkVoice.Stop(); |
|
} |
|
if (ViveInput.GetPressUp(HandRole.RightHand, ControllerButton.Pad)) |
|
{ |
|
walkVoice.Stop(); |
|
runVoice.Stop(); |
|
} |
|
|
|
if (ViveInput.GetPress(HandRole.RightHand, ControllerButton.Menu)) { |
|
//SceneManager.LoadScene(1); |
|
} |
|
} |
|
|
|
void Walk() { |
|
float x_move = camera_.transform.forward.x * Time.deltaTime * walkSpeed; |
|
float z_move = camera_.transform.forward.z * Time.deltaTime * walkSpeed; |
|
if (ViveInput.GetPadAxis(HandRole.RightHand, false).y > 0) |
|
{ |
|
player.transform.position += new Vector3(x_move, 0f, z_move); |
|
} |
|
else if (ViveInput.GetPadAxis(HandRole.RightHand, false).y < 0) |
|
{ |
|
player.transform.position -= new Vector3(x_move, 0f, z_move); |
|
} |
|
} |
|
void Run() { |
|
float x_move = camera_.transform.forward.x * Time.deltaTime * runSpeed; |
|
float z_move = camera_.transform.forward.z * Time.deltaTime * runSpeed; |
|
if (ViveInput.GetPadAxis(HandRole.RightHand, false).y > 0) |
|
{ |
|
player.transform.position += new Vector3(x_move, 0f, z_move); |
|
} |
|
else if (ViveInput.GetPadAxis(HandRole.RightHand, false).y < 0) |
|
{ |
|
|
|
player.transform.position -= new Vector3(x_move, 0f, z_move); |
|
} |
|
} |
|
}
|
|
|