Unity3d 统一,在3d等距游戏中尝试射击时出错

Unity3d 统一,在3d等距游戏中尝试射击时出错,unity3d,Unity3d,我正在做一个3d等距游戏,我正在尝试从玩家向操纵杆所指向的方向射击,我希望它在我松开操纵杆时射击。你可以轻松搜索斗殴明星视频,更好地理解我的意思。第一个脚本是操纵杆脚本,第二个脚本是射击脚本(我把它放在播放器里面)。现在它给了我一个错误:你想要实例化的对象是空的 操纵杆的脚本: using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections; public cla

我正在做一个3d等距游戏,我正在尝试从玩家向操纵杆所指向的方向射击,我希望它在我松开操纵杆时射击。你可以轻松搜索斗殴明星视频,更好地理解我的意思。第一个脚本是操纵杆脚本,第二个脚本是射击脚本(我把它放在播放器里面)。现在它给了我一个错误:你想要实例化的对象是空的

操纵杆的脚本:

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

public class rightjoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;

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

public virtual void OnDrag(PointerEventData ped)
{
    Vector2 pos;
    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);

        inputVector = new Vector3(pos.x * 2, 0, pos.y * 2);
        inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;

        // Move joystickImg
        joystickImg.rectTransform.anchoredPosition =
            new Vector3(inputVector.x * bgImg.rectTransform.sizeDelta.x / 3
                , inputVector.z * (bgImg.rectTransform.sizeDelta.y / 3));

    }
}

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

public virtual void OnPointerUp(PointerEventData ped)
{
    inputVector = Vector3.zero;
    joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}

public float Horizontal()
{
    if (inputVector.x != 0)
        return inputVector.x;
    else
        return Input.GetAxis("Horizontal");
}

public float Vertical()
{
    if (inputVector.z != 0)
        return inputVector.z;
    else
        return Input.GetAxis("Vertical");
}
}
我解决了这个问题:

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

public class shoot : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;
public Rigidbody proiettile;
private Vector3 dir = Vector3.zero;
private Vector3 newpos;
public float launchForce;
public Rigidbody Player;
private Rigidbody clone;

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

public virtual void OnDrag(PointerEventData ped)
{
    Vector2 pos;
    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);

        inputVector = new Vector3(pos.x * 2 +1, 0, pos.y * 2 - 1);
        inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;

        // Move joystickImg
        joystickImg.rectTransform.anchoredPosition =
            new Vector3(inputVector.x * bgImg.rectTransform.sizeDelta.x / 3
                , inputVector.z * (bgImg.rectTransform.sizeDelta.y / 3));

    }
}

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

public virtual void OnPointerUp(PointerEventData ped)
{
    dir.x = Horizontal();
    dir.z = Vertical();
    newpos = dir * (launchForce);
    clone = Instantiate(proiettile, Player.transform.position, Player.transform.rotation);

    clone.transform.position=Vector3.MoveTowards(Player.transform.position,newpos,10f);
    // joystick come back to start position
    inputVector = Vector3.zero;
    joystickImg.rectTransform.anchoredPosition = Vector3.zero;
    clone.timeoutDestructor = 5;
}

public float Horizontal()
{
    if (inputVector.x != 0)
        return inputVector.x;
    else
        return Input.GetAxis("Horizontal");
}

public float Vertical()
{
    if (inputVector.z != 0)
        return inputVector.z;
    else
        return Input.GetAxis("Vertical");
}
}
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.EventSystems;
使用系统集合;
公共类射击:单行为,IDragHandler,IPointerUpHandler,IPointerDownHandler
{
私密图像;
个人形象;
私有向量3输入向量;
公共刚体项目;
私有向量3 dir=向量3.0;
私有向量3 newpos;
公共浮动发射部队;
公共刚体运动员;
私有刚体克隆;
私有void Start()
{
bgImg=GetComponent();
joystickImg=transform.GetChild(0.GetComponent();
}
公共虚拟void OnDrag(PointerEventData-ped)
{
向量2位;
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);
输入向量=新向量3(位置x*2+1,0,位置y*2-1);
inputVector=(inputVector.magnitude>1.0f)?inputVector.normalized:inputVector;
//移动操纵杆
joystickImg.rectTransform.anchoredPosition=
新Vector3(inputVector.x*bgImg.rectTransform.sizeDelta.x/3
,inputVector.z*(bgImg.rectTransform.sizeDelta.y/3);
}
}
POINTERDOWN上的公共虚拟无效(PointerEventData ped)
{
昂德拉格(ped);
}
POINTERUP上的公共虚拟无效(PointerEventData ped)
{
dir.x=水平();
dir.z=垂直();
newpos=dir*(启动力);
克隆=实例化(proitetile,Player.transform.position,Player.transform.rotation);
clone.transform.position=Vector3.movetoward(Player.transform.position,newpos,10f);
//操纵杆回到起始位置
inputVector=Vector3.0;
joystickImg.rectTransform.anchoredPosition=Vector3.0;
clone.timeoutDestructor=5;
}
公共浮动水平()
{
如果(inputVector.x!=0)
返回inputVector.x;
其他的
返回输入。GetAxis(“水平”);
}
公众浮标垂直()
{
如果(inputVector.z!=0)
返回inputVector.z;
其他的
返回输入。GetAxis(“垂直”);
}
}

proiettile是一个刚体,它是一个组件而不是游戏对象。。您需要实例化游戏对象
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class shoot : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;
public Rigidbody proiettile;
private Vector3 dir = Vector3.zero;
private Vector3 newpos;
public float launchForce;
public Rigidbody Player;
private Rigidbody clone;

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

public virtual void OnDrag(PointerEventData ped)
{
    Vector2 pos;
    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);

        inputVector = new Vector3(pos.x * 2 +1, 0, pos.y * 2 - 1);
        inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;

        // Move joystickImg
        joystickImg.rectTransform.anchoredPosition =
            new Vector3(inputVector.x * bgImg.rectTransform.sizeDelta.x / 3
                , inputVector.z * (bgImg.rectTransform.sizeDelta.y / 3));

    }
}

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

public virtual void OnPointerUp(PointerEventData ped)
{
    dir.x = Horizontal();
    dir.z = Vertical();
    newpos = dir * (launchForce);
    clone = Instantiate(proiettile, Player.transform.position, Player.transform.rotation);

    clone.transform.position=Vector3.MoveTowards(Player.transform.position,newpos,10f);
    // joystick come back to start position
    inputVector = Vector3.zero;
    joystickImg.rectTransform.anchoredPosition = Vector3.zero;
    clone.timeoutDestructor = 5;
}

public float Horizontal()
{
    if (inputVector.x != 0)
        return inputVector.x;
    else
        return Input.GetAxis("Horizontal");
}

public float Vertical()
{
    if (inputVector.z != 0)
        return inputVector.z;
    else
        return Input.GetAxis("Vertical");
}
}