我遇到了一个错误,我的操纵杆没有保留在cameraView中

时间:2019-04-29 12:39:39

标签: unity3d joystick

玩游戏时,脚本出现错误,操纵杆仅在左上角运行,可能无法触及。我是从互联网上获得此脚本的,如果您知道其他任何可以正常运行的脚本,请提供帮助。我试图使播放器左右移动,但是不能用按钮播放。我只发现了带有Rigidbody2d的脚本,而我的脚本是3d

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Move : MonoBehaviour
{
    public Transform player;
    public float speed = 5.0f;
    private bool touchStart = false;
    private Vector2 pointA;
    private Vector2 pointB;

    public Transform FixedJoystick;
   

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));

           
           FixedJoystick.transform.position = pointA * -1;
            GetComponent<FixedJoystick>().enabled = true;

        }
        if (Input.GetMouseButton(0))
        {
            touchStart = true;
            pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
        }
        else
        {
            touchStart = false;
        }

    }
    private void FixedUpdate()
    {
        if (touchStart)
        {
            Vector2 offset = pointB - pointA;
            Vector2 direction = Vector2.ClampMagnitude(offset, 1.0f);
            moveCharacter(direction * -1);

           FixedJoystick.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * -1;
        }
        else
        {
            FixedJoystick.GetComponent<FixedJoystick>().enabled = false;

        }

    }
    void moveCharacter(Vector2 direction)
    {
        player.Translate(direction * speed * Time.deltaTime);
    }
}

Picture before starting the game Picture after starting the game

0 个答案:

没有答案