C# CrossplatformInput操纵杆无法统一工作
我已经从unity store的标准资源库连接了一个操纵杆,但是输入没有被读取。汽车可以通过箭头键控制,但操纵杆不工作 这是我的密码C# CrossplatformInput操纵杆无法统一工作,c#,unity3d,augmented-reality,arkit,C#,Unity3d,Augmented Reality,Arkit,我已经从unity store的标准资源库连接了一个操纵杆,但是输入没有被读取。汽车可以通过箭头键控制,但操纵杆不工作 这是我的密码 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput; public class rearwheeldrive : MonoBehaviour { public
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class rearwheeldrive : MonoBehaviour
{
public float maxAngle = 30;
public float maxTorque = 300;
public WheelCollider[] wheelArray;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float angle = maxAngle * CrossPlatformInputManager.GetAxis("Horizontal");
float torque = maxTorque * CrossPlatformInputManager.GetAxis("Vertical");
wheelArray[0].steerAngle = angle;
wheelArray[1].steerAngle = angle;
wheelArray[2].motorTorque = torque;
wheelArray[3].motorTorque = torque;
foreach (WheelCollider wheelcollider in wheelArray)
{
Vector3 P;
Quaternion Q;
wheelcollider.GetWorldPose(out P, out Q);
Transform wheelshape = wheelcollider.transform.GetChild(0);
wheelshape.position = P;
wheelshape.rotation = Q;
}
}
}
即使更改了maxAngle*Input.GetAxis(“水平”);到maxAngle*CrossPlatformInputManager.GetAxis(“水平”);当操纵杆不工作时,它接受箭头键输入
操纵杆对象代码
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace UnityStandardAssets.CrossPlatformInput
{
public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public enum AxisOption
{
// Options for which axes to use
Both, // Use both
OnlyHorizontal, // Only horizontal
OnlyVertical // Only vertical
}
public int MovementRange = 100;
public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
Vector3 m_StartPos;
bool m_UseX; // Toggle for using the x axis
bool m_UseY; // Toggle for using the Y axis
CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
void OnEnable()
{
CreateVirtualAxes();
}
void Start()
{
m_StartPos = transform.position;
}
void UpdateVirtualAxes(Vector3 value)
{
var delta = m_StartPos - value;
delta.y = -delta.y;
delta /= MovementRange;
if (m_UseX)
{
m_HorizontalVirtualAxis.Update(-delta.x);
}
if (m_UseY)
{
m_VerticalVirtualAxis.Update(delta.y);
}
}
void CreateVirtualAxes()
{
// set axes to use
m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
// create new axes based on axes to use
if (m_UseX)
{
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
if (m_UseY)
{
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
}
}
public void OnDrag(PointerEventData data)
{
Vector3 newPos = Vector3.zero;
if (m_UseX)
{
int delta = (int)(data.position.x - m_StartPos.x);
delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
newPos.x = delta;
}
if (m_UseY)
{
int delta = (int)(data.position.y - m_StartPos.y);
delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
newPos.y = delta;
}
transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
UpdateVirtualAxes(transform.position);
}
public void OnPointerUp(PointerEventData data)
{
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
}
public void OnPointerDown(PointerEventData data) { }
void OnDisable()
{
// remove the joysticks from the cross platform input
if (m_UseX)
{
m_HorizontalVirtualAxis.Remove();
}
if (m_UseY)
{
m_VerticalVirtualAxis.Remove();
}
}
}
}
像这样使用它:
public Joystick mJoystick;
float angle = maxAngle * mJoystick.Horizontal;
float torque = maxTorque * mJoystick.Vertical;
编辑:我使用了相同的资源,使用以下设置可以正常工作
像这样使用它:
public Joystick mJoystick;
float angle = maxAngle * mJoystick.Horizontal;
float torque = maxTorque * mJoystick.Vertical;
编辑:我使用了相同的资源,使用以下设置可以正常工作
“操纵杆”不包含“水平”的定义,并且找不到可访问的扩展方法“水平”接受类型为“操纵杆”的第一个参数(是否缺少using指令或程序集引用?)将“操纵杆”替换为附加在层次结构中操纵杆对象上的脚本。附加脚本的名称为“操纵杆”编辑您的问题,并在选择操纵杆对象时附上1-检查员屏幕截图。2-操纵杆脚本“我已经编辑了答案。请看一看。你的第一种方法应该有效。请尝试进行一些调试,以查看操纵杆是否像我一样在空场景中工作。“操纵杆”不包含“水平”的定义,并且找不到接受“操纵杆”类型的第一个参数的可访问扩展方法“水平”(是否缺少using指令或程序集引用?)将“操纵杆”替换为附加在层次结构中操纵杆对象上的脚本。附加脚本的名称为“操纵杆”。编辑您的问题,并在选择操纵杆对象时附加1-检查员屏幕截图。2-“操纵杆脚本”我已经编辑了答案。请看一看。你的第一种方法应该有效。尝试一些调试,看看操纵杆是否能像我一样在空场景中工作。