第三天,我忍受了手机游戏的控制。我想通过操纵杆实现控制。
我希望在游戏中拥有与《色彩之路》一样的控制权-https://vk.com/video174341022_456239047
我在这里(通过操纵杆)-https://vk.com/video174341022_456239048
区别在于,在游戏中,Color Road汉堡包可以保持在中心而没有任何拉力,从屏幕边缘转到中心非常方便。在我的控制下,要保持在屏幕中央,您需要付出很多努力,因为如果将操纵杆向左稍微向左推,三角形就会飞到屏幕的边缘,并且如果您不让操纵杆向左移动,它将以乌龟的速度移动。
我的操纵杆代码:
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler {
private Image backgroundImage;
private Image joystickImage;
private Vector3 inputVector;
private void Start()
{
backgroundImage = GetComponent<Image>();
joystickImage = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnDrag(PointerEventData eventData)
{
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(backgroundImage.rectTransform, eventData.position,
eventData.pressEventCamera, out pos))
{
pos.x = (pos.x / (backgroundImage.rectTransform.sizeDelta.x));
//pos.y = (pos.y / backgroundImage.rectTransform.sizeDelta.y);
inputVector = new Vector3(pos.x * 2f, 0, 0);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
joystickImage.rectTransform.anchoredPosition = new Vector3(inputVector.x * (backgroundImage.rectTransform.sizeDelta.x / 2),
inputVector.z * inputVector.y);
}
}
public virtual void OnPointerDown(PointerEventData eventData)
{
OnDrag(eventData);
}
public virtual void OnPointerUp(PointerEventData eventData)
{
inputVector = Vector3.zero;
joystickImage.rectTransform.anchoredPosition = Vector3.zero;
}
public float Horizontal()
{
if (inputVector.x != 0)
{
return inputVector.x;
}
else
{
return Input.GetAxis("Horizontal");
}
}
}
控制三角形:
public class PlayerMover : MonoBehaviour {
public VirtualJoystick joystick;
private Vector3 MoveVector;
private Rigidbody2D rigidbody;
public float moveSpeed = 10;
void Start()
{
rigidbody = gameObject.GetComponent<Rigidbody2D>();
}
void Update()
{
MoveVector = PoolInput();
Move();
}
private void Move()
{
rigidbody.velocity = Vector2.zero;
rigidbody.velocity = ((MoveVector * moveSpeed));
}
private Vector3 PoolInput()
{
Vector3 dir = Vector3.zero;
dir.x = joystick.Horizontal();
dir.y = 0;
dir.z = 0;
if (dir.magnitude > 1)
dir.Normalize();
Debug.Log("Dir = " + dir);
return dir;
}
}
我找不到任何可解决此问题的信息。到处都是相同的教程。也许这是您需要实现的其他东西?不通过操纵杆?这样的木制控制器对我来说有什么问题?
P.S对不起,我的英语不好