2015-08-31 98 views
1

我使用的是统一性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); 
     } 
    } 
} 
+0

你想让'transform.position'平滑移动或'transform.rotation'吗? –

+1

可能为你节省一点代码的东西是在空变换下父屏蔽,然后在播放器下面居中和父变换空变换。这样,你不需要弄清盾的旋转/位置,只需要旋转它的父母。 (只是提供替代您当前的方法。) – Serlite

+0

@StevenMills transform.position。本质上,如果盾牌位于右下角,并且我将棍子直接移动到左上角,盾牌从左下角消失并立即出现在右上角,而不是平滑地绕着圆圈移动,直到到达新点。 –

回答

0

试试这个。通过对Input.GetAxis("...")进行如此多的方法调用,您真的只需要在开始时调用它一次,并将其缓存到变量中以加快速度。你也不需要两次检查if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f),所以我把所有东西都放到了其中一个检查中。我加了一个乘以Time.smoothDeltaTime有希望出来打圆场你

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); 
} 
+0

感谢您提供方法调用的提示,现在我的代码看起来好多了!然而添加Time.smoothDeltaTime没有为改变角度做任何事情,并打破了我的旋转xD –

+0

啊..没有测试它。它没有它的工作吗? – maksymiuk

+0

该代码的作品,但只有像以前一样,没有平滑的运动围绕曲线的圆圈 –

0

一番搜索和一点帮助,我终于解决了这个问题,感谢球员后:)这是我想出了

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; 
public float circSpeed = 0.1f; // Speed Shield moves around ship 


// Use this for initialization 
void Start() { 

} 

// Update is called once per frame 
void Update() { 

    float rh = Input.GetAxisRaw("rightH"); 
    float rv = Input.GetAxisRaw("rightV"); 

    if(Mathf.Abs(rh) > 0.15f || Mathf.Abs(rv) > 0.15f) 
    { 
     Vector3 targetDir = new Vector3(rh, rv, 0.0f); 
     direction = Vector3.Slerp(transform.position-target.position, targetDir, circSpeed); 

    } 

    Ray ray = new Ray(target.position, direction); // cast ray in direction of point on circle shield is to go 

    transform.position = ray.GetPoint(distance); //move shield to the ray as far as the radius 

    float angleY = transform.position.y - target.position.y; 
    float angleX = -(transform.position.x - target.position.x); 

    float angle = Mathf.Atan2 (angleY, angleX) * Mathf.Rad2Deg-90; //Get angle 

    if(Mathf.Abs(rh) > 0.15f || Mathf.Abs(rv) > 0.15f) 
    { 
     transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1); // keep shield facing outwards in respect to player, will need revisiting on animating shield properly 
    } 


} 
解决方案

}