[英]Is it Possible Have AddForce Method Affect Only The Object i am calling it from in unity 3D?
When I use the AddForce()
Method it affects all the Game Objects in the direction of force vector i have specified which is kinda how physics works in real life, but i do not want that for my game, i want it to have an effect only on the object i am calling it from (ex. ExampleGameObject.GetComponent<Rigidbody>().AddForce();
当我使用AddForce()
方法时,它会影响我指定的力矢量方向上的所有游戏对象,这有点像物理在现实生活中的工作方式,但我不希望我的游戏使用它,我希望它有效果我只在 object 上调用它(例如ExampleGameObject.GetComponent<Rigidbody>().AddForce();
I want it to have an effect on the "ExampleGameObject" Only I hope my question is clear, thanks in advance.我希望它对“ExampleGameObject”产生影响只是我希望我的问题很清楚,在此先感谢。
yourObject.GetComponent<RigidBody>().AddForce(yourForce);
If "yourObject" has children, those children will move together with "yourObject".如果“yourObject”有孩子,这些孩子将与“yourObject”一起移动。 If that's the case you shouldn't use "yourObject" as a parent, or if it's really necessary you could do something similar to this:如果是这种情况,您不应该使用“yourObject”作为父级,或者如果确实有必要,您可以执行类似的操作:
GameObject[] yourChildren;
Transform[] yourChildrenPos;
yourChildren = yourObject.GetComponentsInChildren<Transform>().gameObject; //There is for sure a much better syntax for it, but i forgot it.
yourChildrenPos = yourObject.GetComponentsInChildren<Transform>();
for (int i = 0; i > yourChildren.Length; i ++)
{
yourChildren[i].transform.position = yourChildrenPos[i].position;
}
You just have to assign yourChildrenPos every time before you will move yourObject, and go trough that for loop every time after you moved yourObject.您只需在每次移动您的对象之前分配您的ChildrenPos,并且每次移动您的对象后,go 都会通过 for 循环。 You can easily create a method for that.您可以轻松地为此创建一个方法。 That wont be very effective and will cost a lot of processing power in a large project, but if the project isn't that big, this should do the job.这不会很有效,并且会在大型项目中花费大量的处理能力,但如果项目不是那么大,这应该可以完成工作。 I hope I helped.我希望我有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.