using System.Collections; using System.Collections.Generic; using UnityEngine; /* * 挂载在人物 * * 摇杆控制,需要人物有Rigidbody * 需要有摇杆 */ public class RockerControl : MonoBehaviour { public float moveSpeed = 10f;//移动速度 private RectTransform MoveJoystick;//摇杆UI private FixedJoystick VarJoy;//摇杆 private Rigidbody rb;//刚体 // Start is called before the first frame update void Start() { Init(); } // Update is called once per frame void Update() { Move(); } void Init() { rb = transform.GetComponent(); MoveJoystick = GameObject.Find("MoveJoystick").GetComponent(); VarJoy = MoveJoystick.GetComponent(); } void Move() { //根据摇杆方向设置目标移动的方向, Vector3 direction = Vector3.forward * VarJoy.Vertical + Vector3.right * VarJoy.Horizontal; //或者直接控制目标位置信息来移动目标 transform.position += direction * moveSpeed * Time.fixedDeltaTime; } }