简体   繁体   English

AddForce 方法是否可能仅影响我在统一 3D 中调用它的 Object?

[英]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.

相关问题 无法在 Unity3D 中向我的 object 添加Force - Unable to addForce to my object in Unity3D 具有对象随机方向的Unity 2D addforce - Unity 2D addforce with random direction for the object unity 3d:刚体.AddForce(forceDirection.normalized *力)将对象移离父级和关节 - unity 3d: rigidbody.AddForce(forceDirection.normalized * force) moving objects away from parent and joint 使用Unity Editor,如何从计算机上载文件并将其显示在3D对象或平面上? - Using Unity Editor, how do i upload a file from my computer and have it appear on a 3D object or plane? 在Unity2d中,我有一个通过rigibody addforce移动的圆。 如果它是移动平台的子代,它会变形并且秤会变得怪异 - In Unity2d I have a circle which moves by rigibody addforce. If it a child of a moving platform it deforms and the scale goes weird Unity 3D旋转另一个对象内部的对象,而不会同时影响 - Unity 3D rotate an object inside another object without affect both 方法必须有返回类型(Unity 3D) - Method must have a return type (Unity 3D) 为什么在Camera Script Unity 3D中出现此错误? - Why am I getting this error in Camera Script Unity 3D? Unity:3D运动/碰撞检测失败(AddForce,MovePosition,transform.localPosition) - Unity: 3D movement/Collision detection failure (AddForce, MovePosition, transform.localPosition) 如何在 Unity 3D 中使用 AddForce 和 ForceMode.VelocityChange 来限制速度 - How to cap velocity in Unity 3D using AddForce with ForceMode.VelocityChange
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM