运动虫停止运动

时间:2015-09-23 16:20:28

标签: c# unity3d 2d

以下是该问题的视频:

https://pbs.twimg.com/tweet_video/CPmbdpMWIAAUesX.mp4

问题是当船舶朝着盾牌的方向飞行时,它似乎会定期停止。

这里是船舶和盾牌的代码:

public class MoveOnAxis : MonoBehaviour {

public float speed = 1.0f;
public float defaultDrag = 0.0f;
public float maxDrag = 3.0f;
public float maxAngularDrag = 3.0f;
public float maxTumble = 720;
public Transform shield;

private float distance;
private float hAxis;
private float deadzone = 0.15f;
private float aVel;

private Rigidbody2D rb;

// Use this for initialization
void Start () 
{
    rb = GetComponent<Rigidbody2D>();
}

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

    hAxis = Input.GetAxis("Horizontal");
    aVel = rb.angularVelocity;

    if(Mathf.Abs(hAxis) > 0.15f) //Controlls ship rotation
    {
        if(aVel > 10.0f) //if spinning out after a crash clockwise
        {
            if(aVel <= maxTumble && hAxis > deadzone)
            {
                rb.angularVelocity -= hAxis*speed*2;
            }
            else if(aVel < maxTumble && hAxis < -deadzone)
            {
                rb.angularVelocity -= hAxis*speed*2;
            }
            else if(aVel > maxTumble && hAxis < -deadzone)
            {
                rb.angularVelocity = maxTumble;
            }

        }
        else if (aVel < -10.0f) //if spinning out of a crash anti-clockwise
        {
            if(aVel >= -maxTumble && hAxis < -deadzone)
            {
                rb.angularVelocity -= hAxis*speed*2;
            }
            else if(aVel > -maxTumble && hAxis > deadzone)
            {
                rb.angularVelocity -= hAxis*speed*2;
            }
            else if(aVel < -maxTumble && hAxis > deadzone)
            {
                rb.angularVelocity = -maxTumble;
            }
        }
        else //if no physics angular momentum go back to using transform for rotation
        {
            rb.angularVelocity = 0;
            transform.Rotate(0.0f, 0.0f, hAxis*-speed);
        }
    }

    if(Input.GetAxis("triggerAxis") > 0.1f) //Controlls ship accelleration and decelleration
    {
        //distance = Mathf.Abs((transform.position - shield.position).magnitude);

        //if (distance <= 1.0f && distance >= 0.6f)
        //{
            rb.AddForce(transform.up * speed * Input.GetAxis("triggerAxis"));
        //}
        //else if (distance < 0.6f || distance > 1.0f)
        //{
        //  rb.velocity = new Vector2(0f,0f);
        //}
    }

    if(Input.GetAxis("triggerAxis") < -0.1f)
    {
        rb.drag = maxDrag*-Input.GetAxis("triggerAxis");
        rb.angularDrag = maxAngularDrag*-Input.GetAxis("triggerAxis");
    }
    else
    {
        rb.drag = defaultDrag;
        rb.angularDrag = defaultDrag;
    }

}
}

public class ShieldMovement : MonoBehaviour {

public Transform target; //player shield is attaced to
public float circSpeed = 0.1f; // Speed Shield moves around ship

private Vector3 direction = Vector3.up;
private float distance = 0.8f; // distance from player so it doesn't clip
private float deadzone = 0.15f;
private float separation;
private bool canMove = true;

private Rigidbody2D tRB;
private Rigidbody2D rb;

// Use this for initialization
void Start () 
{
    tRB = target.GetComponent<Rigidbody2D>();
    rb = GetComponent<Rigidbody2D>();
}

void OnCollisionEnter2D (Collision2D other) 
{
    canMove = false;
}

void OnCollisionStay2d(Collision2D other)
{
    canMove = false;
}

void OnCollisionExit2D (Collision2D other) 
{
    canMove = true;
}

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

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

    separation = Mathf.Abs((transform.position - target.position).magnitude);

    if(Mathf.Abs(rh) > deadzone || Mathf.Abs(rv) > deadzone)
    {
        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
    if(canMove)
    {
        transform.position = ray.GetPoint(distance); //move shield to the ray as far as the distance
    }
    else if(!canMove)
    {

        tRB.velocity = new Vector2(0.0f, 0.0f);
        //rb.velocity = new Vector2(0.0f, 0.0f);
        transform.position = transform.position;

    }
    if(separation != distance) //If the shield get torn away from the ship
    {
        transform.position = ray.GetPoint(distance); //move shield to the ray as far as the distance
    }

    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) > deadzone || Mathf.Abs(rv) > deadzone)
    {
        transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward*-1); // keep shield facing outwards in respect to player
    }


}

}

我已尝试取出停止移动船只的东西,如tRB.velocity = new Vector2(0.0f,0.0f);但它根本没有任何区别。关于我需要做什么的任何想法?

修改

从护罩上拆下对撞机可以解决这个问题所以看起来好像盾牌正在与船碰撞,即使它看起来并不像它并且光线投射应该使它远离船只。不知道从哪里开始。

2 个答案:

答案 0 :(得分:0)

对于物理系统更新,最佳做法是将它们放在FixedUpdate()而不是Update()中。前者可用于改变速度或增加力,后者可用于获取用户的输入。

为防止船舶和盾牌发生碰撞,请在每个船上使用不同的标签,然后比较other.collider.tag以忽略这些碰撞。

Process kali received number -1 from process 0

答案 1 :(得分:0)

似乎盾牌以某种方式与船相撞,一旦我将它们放在单独的非相互作用层上,问题便消失了:D