【发布时间】:2018-03-25 12:07:46
【问题描述】:
我正在尝试从另一个脚本更改下面脚本中“XSensitivity”的值。
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.FirstPerson
{
[Serializable]
public class HeadLook
{
public float XSensitivity = 8f;
public float YSensitivity = 8f;
public float MinimumX = -60F;
public float MaximumX = 60F;
public float xRotRaw;
public float yRotRaw;
public HeadLook (float rotX, float rotY)
{
rotX = XSensitivity;
rotY = YSensitivity;
}
// create an instance (an Object) of the HeadLook class
public HeadLook MyHeadLook = new HeadLook(8,8);
private float xRot;
private float yRot;
private float xRotation;
private float yRotation;
// ----------------------------
public void LookRotation(Transform character, Transform head)
{
yRotRaw = CrossPlatformInputManager.GetAxisRaw("HorizontalLook");
xRotRaw = CrossPlatformInputManager.GetAxisRaw("VerticalLook");
yRot = CrossPlatformInputManager.GetAxisRaw("HorizontalLook") * XSensitivity;
xRot = CrossPlatformInputManager.GetAxisRaw("VerticalLook") * YSensitivity;
yRotation -= yRot * 10 * Time.deltaTime;
yRotation = yRotation % 360;
xRotation += xRot * 10 * Time.deltaTime;
xRotation = Mathf.Clamp(xRotation, MinimumX, MaximumX);
head.localEulerAngles = new Vector3(-xRotation, -0, 0);
character.localEulerAngles = new Vector3(0, -yRotation, 0);
}
// ----------------------------
}
}
我认为这可能有效,但出现错误。
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
private HeadLook m_HeadLook;
void Awake ()
{
m_HeadLook = GetComponent< HeadLook >();
sensitivitySlider.value = m_HeadLook.MyHeadLook.XSensitivity;
}
我得到的错误是.. ArgumentException:GetComponent 要求请求的组件“HeadLook”派生自 MonoBehaviour 或 Component 或者是一个接口。
谢谢。
【问题讨论】:
-
该类不是从
MonoBehaviour派生的,因此您无法获取它的组件,因为它不是组件。尝试查找包含HeadLook实例的脚本。 -
错误所在的行是。 m_HeadLook = GetComponent();
标签: c# class unity3d namespaces