【问题标题】:Unity3D: Adding force based on direction an object is facingUnity3D:根据对象面向的方向添加力
【发布时间】:2015-06-09 20:57:02
【问题描述】:

我正在尝试制作一个类似“蠕虫”的游戏,玩家可以选择对象的位置(玩家可以将对象移动 180 度左右),然后将力添加到对象所在的方向面对。我尝试使用 transform.right 和 transform.forward,但力并没有被驱向物体指向的地方。我环顾四周,仍然没有找到/不明白我能做什么。下面是我用来拍摄物体的代码:

    void shootIt(){
       transform.parent = null;
        isPlaying = true;
        A.isKinematic = false;
        A.AddForce(transform.up*varSpeed*_multiplier);
} //"A" stands for the RigidBody2D component I got by using GetComponent<Rigidbody2D>();

我们一如既往地非常感谢您的帮助。

【问题讨论】:

  • 你能告诉我们你旋转对象的代码吗?
  • 它不旋转,但使用数学函数使用 x、y 和 z 轴的 localPosition 将对象移动大约 180 度
  • 啊,我明白了,也许你不想将它移动到它的方向。 1 秒

标签: c# unity3d transform


【解决方案1】:

试试这个:

void shootIt()
{
    Vector2 direction = transform.localPosition.normalized;
    transform.parent = null;
    isPlaying = true;
    A.isKinematic = false;
    A.AddForce(direction*varSpeed*_multiplier);
} 

还可以考虑强迫自己为变量写好名称。 A 描述性不强。

【讨论】:

  • 您刚刚解决了一个我多年来一直在寻找的问题。我不能感谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多