将游戏对象绕玩家对象绕一个平滑的圆圈移动 [英] Moving gameobject in a smooth circle around the player object
问题描述
如果使用的是统一性5.1.2,我会使用它.
I'm using unity 5.1.2 if that makes a difference.
我有一个游戏对象护盾,我想绕着玩家围成一圈移动.我已经将其工作到一定程度,盾牌对输入的响应很好,但动画效果不佳,因为它只是传送到新位置,而不是平滑旋转一圈.游戏是2D自上而下的,因此只能在x/y平面上工作.尝试使用lerp和slerp,但没有获得任何乐趣
I have a game object shield, that I want to move in a circle around a player. I have it working to a degree, the shield responds well to input but is not animated in that it just teleports to the new position instead of moving around in a circle in a smooth rotation. The game is 2D top down so working in the x/y plane only. Have tried to use lerp and slerp but not getting any joy
非常感谢您帮助我们解决这一问题!
Would really appreciate your help to figure this one out!
这是我到目前为止所拥有的:
Here's what I have so far:
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);
}
}
}
推荐答案
尝试一下.如此众多的方法调用Input.GetAxis("...")
,您真的只想在开始时就调用一次并将其缓存到变量中以加快处理速度.另外,您实际上不需要两次检查if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
,因此我将所有内容都放入其中一项检查中.我加了Time.smoothDeltaTime
乘以希望为您解决问题
Try this. With so many method calls to Input.GetAxis("...")
you really want to just call it once in the beginning and cache it to a variable to speed things up. Also you don't really need to check if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
twice, so i put everything into one of the checks. I added a multiplication by Time.smoothDeltaTime
to hopefully smooth things out for you
float h = Input.GetAxis("rightH");
float v = Input.GetAxis("rightV");
float angle = Mathf.Atan2(h, v) * Mathf.Rad2Deg;
if(h != 0f || v != 0f)
{
float step = Time.smoothDeltatime * speed;
direction = Vector3.RotateTowards(transform.forward, new Vector2(h, v).normalized, step, 0.0F);
Ray2D ray = new Ray2D(target.position, direction);
transform.position = ray.GetPoint(distance);
transform.rotation = Quaternion.AngleAxis(angle, -Vector3.forward);
}
这篇关于将游戏对象绕玩家对象绕一个平滑的圆圈移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!