简体   繁体   English

Unity5的VirtualJoystick问题

[英]VirtualJoystick issues with unity5

I have two problems with implementing two Virtual Joysticks in Unity 5: one for player movement (grey color) and the second for camera (orange color) please see the following screenshot: 在Unity 5中实现两个虚拟游戏杆时,我遇到两个问题:一个用于播放器移动(灰色),另一个用于摄像头(橙色),请参见以下屏幕截图:

在此处输入图片说明

My problems: 我的问题:

  1. For the Virtual Joystick responsible for player movement, the player object does not rotate in the same direction the Joystick is pressed , it's moving in all directions but not facing the same direction that the Joystick is pressed. 对于负责玩家移动的虚拟游戏杆,玩家对象的旋转方向并非与操纵杆相同,它在所有方向上均向移动,但并不与操纵杆相同。
  2. The camera can see through the terrain/ground. 摄像机可以看到地形/地面。 How do I prevent that? 我该如何预防?

The script I'm using from Virtual Joystick : 我从Virtual Joystick使用的脚本:

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems;

public class VirtualJoystick : MonoBehaviour,IDragHandler,IPointerUpHandler,IPointerDownHandler {

    private Image bgImg;
    private Image joystickImg;
    public Vector3 InputDirection{ set; get;}
    // Use this for initialization
    void Start () {
        bgImg = GetComponent<Image> ();
        joystickImg = transform.GetChild (0).GetComponent<Image> ();
        InputDirection = Vector3.zero;

    }

    // Update is called once per frame
    //void Update () {

    //}
    public virtual void OnDrag(PointerEventData ped)
    {
        Vector2 pos = Vector2.zero;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle
            (bgImg.rectTransform,
                ped.position,
                ped.pressEventCamera,
                out pos)) {
            pos.x=(pos.x/bgImg.rectTransform.sizeDelta.x);
            pos.y=(pos.y/bgImg.rectTransform.sizeDelta.y);
            float x=(bgImg.rectTransform.pivot.x==1) ? pos.x*2+1 : pos.x*2-1;
            float y=(bgImg.rectTransform.pivot.y==1) ? pos.y*2+1 : pos.y*2-1;
            InputDirection=new Vector3(x,0,y);
            InputDirection=(InputDirection.magnitude>1) ? InputDirection.normalized : InputDirection;

            joystickImg.rectTransform.anchoredPosition=
                new Vector3(InputDirection.x*(bgImg.rectTransform.sizeDelta.x/3),InputDirection.z*(bgImg.rectTransform.sizeDelta.y/3));
            Debug.Log(InputDirection);
        }

    }
    public virtual void OnPointerDown(PointerEventData ped)
    {
        OnDrag (ped);

    }
    public virtual void OnPointerUp(PointerEventData ped)
    {


        //Here is the problem it just goes to zero so fast so my character also moves so fast...how can i make it so motth
        InputDirection =Vector3.zero;
        joystickImg.rectTransform.anchoredPosition =Vector3.zero;
    }
} 

The script to move the player: 移动播放器的脚本:

using UnityEngine; using System.Collections;

public class Motor : MonoBehaviour {

    public float moveSpeed = 5.0f;
    public float drag = 0.5f;
    public float terminalRotationSpeed = 25.0f;
    private Rigidbody controller;
    private Transform camtransform;
    public VirtualJoystick movejoystick;
    private void Start()
    {
        controller =GetComponent<Rigidbody>();
        controller.maxAngularVelocity = terminalRotationSpeed;
        controller.drag = drag;
        camtransform = Camera.main.transform;
    }
    private void Update ()
    {
        Vector3 dir = Vector3.zero;
        dir.x = Input.GetAxis ("Horizontal");
        dir.z = Input.GetAxis ("Vertical");
        if (dir.magnitude > 1)
            dir.Normalize ();
        if (movejoystick.InputDirection != Vector3.zero) {
            dir = movejoystick.InputDirection;
        }

        Vector3 rotatedDir = camtransform.TransformDirection (dir);
        rotatedDir = new Vector3 (rotatedDir.x, 0, rotatedDir.z);
        rotatedDir = rotatedDir.normalized * dir.magnitude;
        controller.AddForce (dir * moveSpeed);
        Quaternion eulerRot = Quaternion.Euler(0.0f, 0.0f, rotatedDir.x);
        transform.rotation  = Quaternion.Slerp(transform.rotation, eulerRot, Time.deltaTime * 10);
    }
}

The script I'm using for the camera: 我用于相机的脚本:

using UnityEngine;
using System.Collections;

public class FreeCamera :
 MonoBehaviour {
    public Transform lookAt;
    public VirtualJoystick camerajs;
    private float distance = 200.0f;
    private float currentx = 0.0f;
    private float currenty = 0.0f;
    private float sensitivityx = 1.0f;
    private float sensitivityy = 1.0f;
    private void Update()
    {
        currentx += camerajs.InputDirection.x * sensitivityx;
        currenty += camerajs.InputDirection.z * sensitivityy;
    }
    private void LateUpdate()
    {
        Vector3 dir = new Vector3(0, 0, -distance);
        Quaternion rotation = Quaternion.Euler(currenty, currentx, 0);
        transform.position = lookAt.position + rotation * dir;
        transform.LookAt(lookAt);
    }
} 

more screenshots : 更多屏幕截图:

在此处输入图片说明

For question 1: The code below first turns according to joystick-x direction and then move forward/backward according to joystick-y. 对于问题1:以下代码首先根据操纵杆x方向旋转,然后根据操纵杆y向前/向后移动。 In Motor.Update: 在Motor.Update中:

private void Update ()
{
    Vector3 dir = Vector3.zero;
    dir.x = Input.GetAxis ("Horizontal");
    dir.z = Input.GetAxis ("Vertical");
    if (dir.magnitude > 1)
        dir.Normalize ();
    if (movejoystick.InputDirection != Vector3.zero) {
        dir = movejoystick.InputDirection;
    }

    Vector3 rotatedDir = transform.TransformDirection (dir); // world direction of joystick direction
    Vector3 newDir = Vector3.RotateTowards(transform.forward, rotatedDir, Time.deltaTime * 10f, 0.0F); // turn towards joystick direction
    transform.rotation = Quaternion.LookRotation(newDir); // set the new direction
    controller.AddForce (transform.forward * moveSpeed * dir.z); // scale force by joystick up/down
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM