From bdd0c3eee905c5dc6fcf22344f7fdbd4cc3e3a02 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 30 Jan 2026 23:07:07 +0800 Subject: 完成双角色操纵 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/PlayerMovement.cs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Assets/Scripts/PlayerMovement.cs (limited to 'Assets/Scripts/PlayerMovement.cs') diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs new file mode 100644 index 0000000..90252b3 --- /dev/null +++ b/Assets/Scripts/PlayerMovement.cs @@ -0,0 +1,37 @@ +using System; +using UnityEngine; + +[RequireComponent(typeof(PlayerControl))] +[RequireComponent(typeof(Rigidbody))] +public class PlayerMovement : MonoBehaviour +{ + public float movementSpeed; + public float movementSpeedOnGrabbing; + + private PlayerControl _playerControl; + private Rigidbody _rigidbody; + + private void Awake() + { + _playerControl = GetComponent(); + _rigidbody = GetComponent(); + } + + private void FixedUpdate() + { + FixedUpdateMovement(); + } + + private void FixedUpdateMovement() + { + var velocity = _rigidbody.linearVelocity; + var speed = _playerControl.grabbing ? movementSpeedOnGrabbing : movementSpeed; + var direction = new Vector3( + _playerControl.movementHorizontal, + 0, + _playerControl.movementVertical) + .normalized; + var playerVelocity = speed * direction + velocity.y * Vector3.up; + _rigidbody.linearVelocity = playerVelocity; + } +} -- cgit