【发布时间】:2015-08-31 15:00:02
【问题描述】:
如果有区别,我正在使用 unity 5.1.2。
我有一个游戏对象盾牌,我想绕着玩家转一圈。我让它在一定程度上工作,盾牌对输入的反应很好,但没有动画,因为它只是传送到新的位置,而不是在一个圆中平滑旋转。游戏是 2D 自上而下的,因此只能在 x/y 平面上工作。尝试使用 lerp 和 slerp 但没有得到任何乐趣
非常感谢您帮助解决这个问题!
这是我目前所拥有的:
public class ShieldMovement : MonoBehaviour {
public Transform target; //player shield is attaced to
float distance = 0.8f; // distance from player so it doesn't clip
Vector3 direction = Vector3.up;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float angle = Mathf.Atan2 (Input.GetAxisRaw("rightH"), Input.GetAxisRaw("rightV"))* Mathf.Rad2Deg;
if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
{
direction = new Vector3(Input.GetAxis("rightH"),Input.GetAxis("rightV"), 0.0f ) ;
}
Ray ray = new Ray(target.position, direction);
transform.position = ray.GetPoint(distance);
if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
{
transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1);
}
}
}
【问题讨论】:
-
你是想让
transform.position顺利移动还是transform.rotation? -
可以为您节省一些代码的方法是将盾牌放在空变换下,然后将空变换置于播放器下的中心和父级。这样,您不需要弄清楚盾牌的旋转/位置,只需弄清楚其父级的旋转即可。 (只是提供您当前方法的替代方案。)
-
@StevenMills transform.position。本质上,如果盾牌位于右下角,并且我将摇杆直接移到左上角,则盾牌会从左下角消失并立即出现在右上角,而不是平稳地绕圈移动直到到达新点。跨度>
标签: c# unity3d rotation jquery-animate