首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VirtualJoystick与unity5的问题

VirtualJoystick与unity5的问题
EN

Stack Overflow用户
提问于 2017-12-19 18:52:46
回答 1查看 191关注 0票数 0

在统一5中实现两个虚拟游戏有两个问题:一个用于玩家的移动(灰色),另一个用于照相机(橙色),请参见下面的屏幕截图:

我的问题:

  1. 对于负责玩家移动的虚拟游戏棒,播放机对象不按同一方向旋转-- player被按下,它向所有方向移动,但面对的方向与按下player的方向不同。
  2. 照相机能透过地形/地面看清楚。我怎么才能防止这种情况?

我从Virtual中使用的脚本:

代码语言:javascript
复制
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;
    }
} 

移动播放器的脚本:

代码语言:javascript
复制
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);
    }
}

我用来拍相机的剧本:

代码语言:javascript
复制
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);
    }
} 

更多截图:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-21 07:42:22

对于问题1:下面的代码首先根据操纵杆-x方向旋转,然后根据操纵杆-y向前/向后移动。在Motor.Update中:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47893446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档