unity3d - Unity,尝试在 3d 等距游戏中射击时出错
问题描述
我正在制作一个 3d 等距游戏,我正在尝试从玩家向操纵杆的方向射击,我希望它在我释放操纵杆时射击。您可以轻松搜索 Brawl Stars 视频以更好地理解我的意思。第一个脚本是操纵杆的,第二个是拍摄的(我把它放在播放器里面)。现在它给了我这个错误:你要实例化的对象是空的。
操纵杆的脚本:
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);
}
}
解决方案
我解决了这个问题:
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");
}
}
推荐阅读
- c# - SpeechSynthesizer.SelecVoice(字符串)不工作
- vue.js - 如何在 VueJS 中使用带有渲染功能的滚动?
- php - 使用我的插件功能将表单数据放入我的数据库
- java - Jetty 9 - ServletContextListener 中缺少 ServerContainer 属性
- java - 更新后系统库在eclipse中不起作用
- omnet++ - 向 TraCICommandInterface 添加一个函数
- selenium - Selenium:在没有输入标签的字段中输入文本 - 如何找到它?
- database - Yii Query DB when `where` 有时可以是全部(*)(必要时省略一个或多个 where 子句)
- sql-server - 生成序列结果来自:MS SQL 中的当前日期 + (max_number + 1)
- java - 如何修复 Java 程序中损坏的数学以获取更改