如何用操纵杆旋转fps播放器枪和相机。?

时间:2017-06-24 11:34:29

标签: c# unity3d unity5 joystick

我是个新手。我用android设备创建fps游戏。我有一个简单的代码,使用下面发布的操纵杆移动玩家。我想像玩电脑游戏一样旋转玩家和相机(反击)。我不知道如何实现这种类型的玩家轮换。任何人都可以帮助我。

这是我的小脚本代码。

虚拟joystick.cs

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

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

private Image bgImg;
private Image joystickImg;

public Vector3 InputDirection{ set; get;}

private void Start()
{
    bgImg = GetComponent<Image> ();
    joystickImg = gameObject.transform.GetChild(0).GetComponent<Image> ();
    InputDirection = Vector3.zero;
}

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("OnDrag " + InputDirection);
    }

}
public virtual void  OnPointerUp(PointerEventData ped)
{
    InputDirection = Vector3.zero;
    joystickImg.rectTransform.anchoredPosition = Vector3.zero;
    Debug.Log("OnPointerUp");
}
public virtual void OnPointerDown(PointerEventData ped)
{   
    OnDrag (ped);
    Debug.Log("OnPointerDown");
}

}

Motor.cs

using UnityEngine;
using System.Collections;

public class Motor : MonoBehaviour {

public float moveSpeed = 5.0f;
public float drag = 5.0f;
public float terminalRotationSpeed = 25.0f;
public VirtualJoystick moveJoystick;


private Rigidbody rgb;
private Transform camTras;
Vector3 dir;

private void Awake()
{
    camTras = Camera.main.transform;
}
// Use this for initialization
private void Start () {
    rgb = GetComponent<Rigidbody> ();
    rgb.maxAngularVelocity = terminalRotationSpeed;
    rgb.drag = drag;    
    dir = Vector3.zero;
}

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

    dir.x = Input.GetAxis ("Horizontal");
    dir.z = Input.GetAxis ("Vertical");

    if (dir.magnitude > 1)
        dir.Normalize ();

    if (moveJoystick.InputDirection != Vector3.zero) {
        dir = moveJoystick.InputDirection;
    }

    //camTras.transform.LookAt (transform);
    //Rotate our direction vector with camera
    Vector3 rotatedDir = camTras.TransformDirection(dir);
    rotatedDir = new Vector3 (rotatedDir.x, rotatedDir.y, rotatedDir.z);
    rotatedDir = rotatedDir.normalized * dir.magnitude;

    rgb.AddForce (rotatedDir * moveSpeed * 20 * Time.deltaTime);

}
} 

1 个答案:

答案 0 :(得分:0)

为什么不按照本教程进行操作,该教程将向您展示您需要做什么

https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial