如何统一改变胶囊对撞机的特点

时间:2017-10-03 17:08:52

标签: unity3d collider

我希望我的问题不重复。 我需要在运行时更改胶囊对撞机的功能 我的代码:

Game_Controller.Database [m, n].Nute_M.AddComponent(typeof(CapsuleCollider));
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().center.y = 0.17f;Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().radius = 0.72f;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().height = 0.64;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().direction = 2;

但是我得到了

`Cannot modify a value type return value of `UnityEngine.CapsuleCollider.center'. Consider storing the value in a temporary variable`

错误。 请帮帮我。

1 个答案:

答案 0 :(得分:0)

CapsuleCollider.centerVector3。您无法修改在另一个类中声明为auto-property的Vector3属性。复制它,修改y轴,然后将其分配回CapsuleCollider.center

此外,CapsuleCollider>().height是一个浮点数,你需要在0.64f的末尾添加f来进行编译。

//Make a copy
Vector3 center = Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().center;
//Modify the copy's y axis
center.y = 0.17f;
//Apply to the target object
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().center = center;

Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().radius = 0.72f;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().height = 0.64f;
Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ().direction = 2;

每次都不需要使用GetComponent,因此您只需缓存CapsuleCollider,因为它是一个类,它将存储引用:

//Get CapsuleCollider collider
CapsuleCollider cCollider = Game_Controller.Database [m, n].Nute_M.GetComponent<CapsuleCollider> ();
//Make a copy
Vector3 center = cCollider.center;
//Modify the copy's y axis
center.y = 0.17f;
//Apply to the target object
cCollider.center = center;

cCollider.radius = 0.72f;
cCollider.height = 0.64f;
cCollider.direction = 2;