来自不同位置的Unity2d拍摄对象

时间:2018-07-27 07:46:51

标签: c# unity3d

我是Unity 2d的新手,我一直在关注有关愤怒的小鸟风格游戏的教程,我想为自己的项目编辑游戏的一些游戏玩法,这是我们从弹射器射击物体时遇到的问题从不同的位置捕获物体,然后从该位置再次射击物体,我正在研究一种自上而下的2d游戏,就像在射击物体并捕获然后再次射击一样。这是我的代码,大部分就像是教程,我有点破坏了编辑

公开课解雇:MonoBehaviour {

public float MaxStretch = 3f;
public LineRenderer CatapultFront;
public LineRenderer CatapultBack;
public Transform catapult;

private bool ClickedOn;
private Rigidbody2D Rigid;
private SpringJoint2D spring;
private Ray RayToMouse;
private Ray LeftCatapultToProjectile;
private Vector2 PreVelocity;
private float CircleRadius;
private float maxS;
private bool entering;




private void Awake()
{

    Rigid = GetComponent<Rigidbody2D>();
    spring = GetComponent<SpringJoint2D>();



}


void Start () {

    catapult = spring.connectedBody.transform;
    LineRendereSetup();
    RayToMouse = new Ray(catapult.transform.position, Vector3.zero);
    LeftCatapultToProjectile = new Ray(CatapultFront.transform.position, Vector3.zero);
        maxS = MaxStretch * MaxStretch;

    CircleCollider2D circle = GetComponent<Collider2D>() as CircleCollider2D;
    CircleRadius = circle.radius;
  Rigid.gravityScale = 0.0f;

}

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


{
    LineRendererUpdate();
    if (ClickedOn)
        Dragging();
        if(spring.enabled==true)
        {

        if (!Rigid.isKinematic  &&  PreVelocity.sqrMagnitude > Rigid.velocity.sqrMagnitude) {

            spring.enabled = false;
                Rigid.velocity = PreVelocity;

            }
        if (!ClickedOn)
        {
          PreVelocity = Rigid.velocity;


            CatapultFront.enabled = false;
            CatapultBack.enabled = false;

        }

    }

}

private void OnMouseDown()
{
              spring.enabled = false;
        ClickedOn = true;
    }

private void OnMouseUp()
{



        spring.enabled = true;
        Rigid.isKinematic = false;
        ClickedOn = false;

}
protected void LineRendereSetup()
{
    CatapultFront.SetPosition(0, CatapultFront.transform.position);
    CatapultBack.SetPosition(0, CatapultBack.transform.position);

    CatapultFront.sortingLayerName = "foreground";
    CatapultBack.sortingLayerName = "foreground";

    CatapultFront.sortingOrder = 3;
    CatapultBack.sortingOrder = 1;

}
void Dragging()
{

    Vector3 mouseworldpoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector2 CatapultToMouse = mouseworldpoint - catapult.position;
    if(CatapultToMouse.sqrMagnitude > maxS)
    {
        RayToMouse.direction = CatapultToMouse;
        mouseworldpoint = RayToMouse.GetPoint(MaxStretch);

    }
    mouseworldpoint.z = 0f;
    transform.position = mouseworldpoint;
}

private void LineRendererUpdate()     {

    Vector2 CatapultToProjectile = transform.position - CatapultFront.transform.position;
    LeftCatapultToProjectile.direction = CatapultToProjectile;
    Vector2 HoldPoint = LeftCatapultToProjectile.GetPoint(CatapultToProjectile.magnitude +CircleRadius);
    CatapultFront.SetPosition(1,HoldPoint);
    CatapultBack.SetPosition(1, HoldPoint);
}
private void OnTriggerEnter2D(Collider2D collision)
{
    catapult.position = collision.transform.position;
    spring.connectedBody = null;
    spring.connectedBody = collision.GetComponent<Rigidbody2D>();
    OnMouseDown();
    OnMouseUp();

}

}

0 个答案:

没有答案