_xiaofang/xiaofang/Assets/Script/Character/RockerControl.cs
2024-10-21 10:08:49 +08:00

48 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Rigidbody>();
MoveJoystick = GameObject.Find("MoveJoystick").GetComponent<RectTransform>();
VarJoy = MoveJoystick.GetComponent<FixedJoystick>();
}
void Move()
{
//根据摇杆方向设置目标移动的方向,
Vector3 direction = Vector3.forward * VarJoy.Vertical + Vector3.right * VarJoy.Horizontal;
//或者直接控制目标位置信息来移动目标
transform.position += direction * moveSpeed * Time.fixedDeltaTime;
}
}