From 7f493f682503f5186308de7b8f74b5b49233cfe4 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 2 Nov 2023 11:51:31 +0800 Subject: +init --- .../Rewired.Demos/EightPlayersExample_Player.cs | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Rewired/Rewired.Demos/EightPlayersExample_Player.cs (limited to 'Rewired/Rewired.Demos/EightPlayersExample_Player.cs') diff --git a/Rewired/Rewired.Demos/EightPlayersExample_Player.cs b/Rewired/Rewired.Demos/EightPlayersExample_Player.cs new file mode 100644 index 0000000..0ce56e1 --- /dev/null +++ b/Rewired/Rewired.Demos/EightPlayersExample_Player.cs @@ -0,0 +1,71 @@ +using System; +using UnityEngine; + +namespace Rewired.Demos; + +[AddComponentMenu("")] +[RequireComponent(typeof(CharacterController))] +public class EightPlayersExample_Player : MonoBehaviour +{ + public int playerId; + + public float moveSpeed = 3f; + + public float bulletSpeed = 15f; + + public GameObject bulletPrefab; + + private Player player; + + private CharacterController cc; + + private Vector3 moveVector; + + private bool fire; + + [NonSerialized] + private bool initialized; + + private void Awake() + { + cc = GetComponent(); + } + + private void Initialize() + { + player = ReInput.players.GetPlayer(playerId); + initialized = true; + } + + private void Update() + { + if (ReInput.isReady) + { + if (!initialized) + { + Initialize(); + } + GetInput(); + ProcessInput(); + } + } + + private void GetInput() + { + moveVector.x = player.GetAxis("Move Horizontal"); + moveVector.y = player.GetAxis("Move Vertical"); + fire = player.GetButtonDown("Fire"); + } + + private void ProcessInput() + { + if (moveVector.x != 0f || moveVector.y != 0f) + { + cc.Move(moveVector * moveSpeed * Time.deltaTime); + } + if (fire) + { + UnityEngine.Object.Instantiate(bulletPrefab, base.transform.position + base.transform.right, base.transform.rotation).GetComponent().AddForce(base.transform.right * bulletSpeed, ForceMode.VelocityChange); + } + } +} -- cgit v1.1-26-g67d0