当我将此脚本放置在另一个Hingejoint上时,与每个脚本进行交互都会影响它们。
我该如何做,使其仅影响我正在与之互动的铰链?
我正在使用RayCast确定我首先击中的物体,但是我不想为每个Hingejoint对撞机编写if语句。
public class Hingetest : MonoBehaviour
{
public HingeJoint Door;
public float rotatedoor = 0.0f;
public static bool hasbeenHit = false;
void Update()
{
if (hasbeenHit == true)
{
if (Input.GetMouseButton(0))
{
float h = Input.GetAxis("Horizontal");
rotatedoor = rotatedoor + h;
}
}
rotatedoor = (rotatedoor < 0f) ? 0f : (rotatedoor > 120f) ? 120f : rotatedoor;
JointSpring doorSpring = Door.spring;
doorSpring.targetPosition = rotatedoor;
Door.spring = doorSpring;
}
}
射线广播脚本。
public class RaycastInteraction : MonoBehaviour {
public float distanceToSee;
RaycastHit whatIHit;
public GameObject object1;
public GameObject object2;
// Update is called once per frame
void Update () {
int layerMask = 1 << 8;
layerMask = ~layerMask;
if (Physics.Raycast(this.transform.position, this.transform.forward, out whatIHit, distanceToSee, layerMask))
{
if (whatIHit.collider.gameObject == object1)
{
Debug.Log("hithasbeenmade");
Hingetest.hasbeenHit = true;
}
}
}
}
答案 0 :(得分:2)
hasbeenHit
是static
变量,因此在类型Hingetest
的所有组件之间“共享”。因此,当您将其设置为true
时,就可以为所有Hingetest
组件设置此值!
将其设为普通的public
变量
public bool hasbeenHit = false;
并将其设置为
whatIHit.gameObject.GetComponent<Hingetest>().hasbeenHit = true;
比你还好。
在直接尝试在Unity中进行更改之前,您可能应该退后一步并获得更多的c#基础知识。例如。 c# static