我需要在碰撞时更改其他对象的vitesse,这是我的代码:
void OnControllerColliderHit(ControllerColliderHit hit)
{
Collider collider = hit.collider;
if (!collider.gameObject.name.Equals ("Plane"))
{
CharacterController opponent = collider.GetComponent(typeof(CharacterController)) as CharacterController;
if (opponent == null)
{
return;
}
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
opponent.Move (pushDir * pushPower * Time.deltaTime);
}
}
但这并不是我想要的,实际上,对手上还有一个脚本,我想改变脚本上的一些属性,例如:
opponent.vitesse += pushDir * pushPower * Time.deltaTime;
vitesse是objet对手上的Opponent类的属性之一。 我想要的是当我击中它而不是直接移动它时改变对手的vitesse
答案 0 :(得分:0)
Opponent opponent = collider.GetComponent(typeof(Opponent)) as Opponent;
而不是
CharacterController opponent = collider.GetComponent(typeof(CharacterController)) as CharacterController;
这样你就可以改变对手脚本上的变量
完整代码:
void OnControllerColliderHit(ControllerColliderHit hit)
{
Collider collider = hit.collider;
if (!collider.gameObject.name.Equals ("Plane"))
{
Opponent opponent = collider.GetComponent(typeof(Opponent)) as Opponent;
if (opponent == null)
{
return;
}
opponent.vitesse += pushDir * pushPower * Time.deltaTime;
}
}