我正在Unity平台上制作一个简单的益智游戏。首先,我在头上测试了一些简单的物理,然后用C#编写。但由于某些原因,一些代码并没有产生相同的效果。从头开始,代码使精灵完全按照我的意愿行事;朝着鼠标走,慢下来,然后停在鼠标上。然而,(我认为是)Unity中的相同代码使得精灵变得疯狂并永不停止。
更新
肯定是因为#2。事实证明,精灵正在从其他边界框中反弹,导致速度急剧变化。它可以使用较小的值,除了我有重力。我的新问题是有没有办法阻止精灵弹跳?
Vector2 CalcHookPull(){
//code for finding which hook and force
//find if mouse pressed
if (Input.GetMouseButton(0))
{
//check if new hook needs to be found
if (!last){
Vector2 mypos = new Vector2(transform.position.x, transform.position.y);
Vector2 relmousepos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x-transform.position.x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y-transform.position.y);
//player is on layer 8, don't want to check itself
int layermask = ~(1<<8);
RaycastHit2D hit = Physics2D.Raycast(mypos, relmousepos, Mathf.Infinity, layermask);
maybeHook = hit.collider.gameObject;
}
if(maybeHook.tag == "hook"){
float hookx = maybeHook.transform.position.x;
float hooky = maybeHook.transform.position.y;
float myx = transform.position.x;
float myy = transform.position.y;
//elasticity = 30
float chx = (hookx - myx) / (5f + 1f/3f);
float chy = (hooky - myy) / (5f + 1f/3f);
float curchx = GetComponent<Rigidbody2D> ().velocity.x;
float curchy = GetComponent<Rigidbody2D> ().velocity.y;
Vector2 toChange = new Vector2 (curchx * 0.3f + chx, curchy * 0.3f + chy);
Debug.Log(toChange);
last = Input.GetMouseButton (0);
return toChange;
} else{
last = Input.GetMouseButton (0);
return new Vector2(0,0);
}
} else {
last = Input.GetMouseButton (0);
return new Vector2 (0, 0);
}
}
可能的差异:
感谢任何帮助,谢谢!
答案 0 :(得分:0)