Я играю в 3D-изометрию c и пытаюсь стрелять из игрока в направлении, указанном джойстиком, и мне бы хотелось, чтобы он стрелял, когда я отпускаю джойстик. Вы можете легко найти видео «Звезды потасовки», чтобы лучше понять, что я имею в виду. Первый скрипт для джойстика, второй для съемки (я поместил его в плеер). Теперь он дает мне эту ошибку: объект, который вы хотите создать, имеет значение null.
Сценарий джойстика:
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 System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shoot : MonoBehaviour
{
public Rigidbody proiettile;
public float launchForce = 70f;
public rightjoystick moveJoystick;
private Vector3 dir = Vector3.zero;
public void Update()
{
dir.x = moveJoystick.Horizontal();
dir.z = moveJoystick.Vertical();
}
private void FixedUpdate()
{
var projectileInstance = Instantiate(proiettile);
projectileInstance.AddForce(dir * launchForce);
}
}