當前位置: 首頁>>代碼示例>>C#>>正文


C# Rigidbody2D.AddForce方法代碼示例

本文整理匯總了C#中UnityEngine.Rigidbody2D.AddForce方法的典型用法代碼示例。如果您正苦於以下問題:C# Rigidbody2D.AddForce方法的具體用法?C# Rigidbody2D.AddForce怎麽用?C# Rigidbody2D.AddForce使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在UnityEngine.Rigidbody2D的用法示例。


在下文中一共展示了Rigidbody2D.AddForce方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: OnTriggerEnter2D

 void OnTriggerEnter2D(Collider2D col)
 {
     if (col.tag == "Enemy" && col.gameObject.GetComponent<EnemyController>().timeSinceLastHit > 0.2f) {
         enemyrb2d = col.GetComponent <Rigidbody2D> ();
         col.gameObject.GetComponent<EnemyController>().timeSinceLastHit = 0;
         col.gameObject.GetComponent<EnemyController>().health -= 20;
         int health = col.gameObject.GetComponent<EnemyController>().health;
         if (col.transform.position.x > player.transform.position.x) {
             enemyrb2d.AddForce(new Vector2(50f, 50f));
             if (health <= 0) {
                 enemyrb2d.rotation = -30;
             }
         }
         else if (col.transform.position.x < player.transform.position.x) {
             enemyrb2d.AddForce(new Vector2(-50f, 50f));
             if (health <= 0) {
                 enemyrb2d.rotation = 30;
             }
         }
         if (col.gameObject.GetComponent<EnemyController>().health <= 0) {
             enemyrb2d.freezeRotation = false;
             col.gameObject.GetComponent<BoxCollider2D>().enabled = false;
             col.gameObject.GetComponent<CircleCollider2D>().enabled = false;
             player.GetComponent<PirateController>().kills++;
         }
     }
 }
開發者ID:dunbarj,項目名稱:Firates,代碼行數:27,代碼來源:AttackController.cs

示例2: WallJump

    public void WallJump(Animator myAnimator, Rigidbody2D myRigidbody2D, WallCheck[] allWallChecks)
    {
        if(!lockWallJump)
        {
            foreach(WallCheck localWallCheck in allWallChecks)
            {
                if(localWallCheck.walled)
                {
                    lockWallJump = true;
                    if(myRigidbody2D.transform.localScale.x < 0)
                    {
                        myRigidbody2D.AddForce(new Vector2(myRigidbody2D.velocity.x + jumpForce.x, jumpForce.y));
                    }
                    else
                    {
                        myRigidbody2D.AddForce(new Vector2(myRigidbody2D.velocity.x - jumpForce.x, jumpForce.y));
                    }

                    if(!myAnimator.GetCurrentAnimatorStateInfo(0).IsName(jumpAnimationName))
                    {
                        myAnimator.SetTrigger(jumpAnimationTriggerName);
                    }
                    Invoke ("UnlockWallJump", lockTime);
                    break;
                }
            }
        }
    }
開發者ID:Dunmord,項目名稱:UnityMegamanX,代碼行數:28,代碼來源:WallJump2D.cs

示例3: Start

    void Start()
    {
        myRB = GetComponent<Rigidbody2D>();
        myCL = GetComponent<Collider2D>();
        target = GameObject.FindWithTag("Player").transform;
        absDistance = Mathf.Abs(transform.position.x - target.position.x);
        Power = (1000 * absDistance) / 14;

        if (target.position.x > transform.position.x)
        {
            myRB.AddForce(new Vector2(Power, 0));
            transform.Rotate(new Vector3(0, 0, 270));
        }
        if (target.position.x == transform.position.x)
        {
            myRB.AddForce(new Vector2(0, 100));
            transform.Rotate(new Vector3(0, 0, 270));
        }

        if (target.position.x < transform.position.x)
        {
            myRB.AddForce(new Vector2(-Power, 0));
            transform.Rotate(new Vector3(0, 0, 270));
        }
        Destroy(gameObject, 3);
    }
開發者ID:luliz,項目名稱:brazilball,代碼行數:26,代碼來源:arroww2.cs

示例4: Awake

    // Use this for initialization
    void Awake()
    {
        // Reference to the rigidbody.
        this.mRigidbody2d = GetComponent<Rigidbody2D>();

        //Apply constant force.
        if(transform.localRotation.z > 0)
           mRigidbody2d.AddForce(new Vector2(-1, 0) * mBulletMultiplier , ForceMode2D.Impulse);
        else mRigidbody2d.AddForce(new Vector2(1, 0) * mBulletMultiplier, ForceMode2D.Impulse);
    }
開發者ID:vamidi,項目名稱:Project-Dimensia,代碼行數:11,代碼來源:Bullet.cs

示例5: Start

    // Use this for initialization
    void Start()
    {
        m_Rigidbody2D = GetComponent<Rigidbody2D> ();
        if (leftRight)
            m_Rigidbody2D.AddForce(new Vector2(speed, 0));
        else
            m_Rigidbody2D.AddForce(new Vector2(0, speed));

        originalPosition = m_Rigidbody2D.position;
    }
開發者ID:stults-p,項目名稱:Space-Runner,代碼行數:11,代碼來源:Oscillate.cs

示例6: Start

    // Use this for initialization
    void Start()
    {
        //find all of our components
        body = gameObject.GetComponent<Rigidbody2D>();
        renderedSprite = gameObject.GetComponent<SpriteRenderer>();

        //Set the sprite to the correct type
        renderedSprite.sprite = GameControl.energyBalls[energyBall];

        //start the bullet moving from the get go
        body.AddForce(new Vector2(0, 1) * yForce, ForceMode2D.Impulse);
        body.AddForce(new Vector2(1, 0) * xForce, ForceMode2D.Impulse);
    }
開發者ID:nebathemonk,項目名稱:UnityDemo,代碼行數:14,代碼來源:Bullet.cs

示例7: Start

    public override void Start()
    {
        base.Start ();
        Rb = GetComponent<Rigidbody2D> ();
        if (CharacterBehavior.FacingRight) {
            Rb.AddForce (new Vector2 (.2f, .3f) * speed * 7, ForceMode2D.Impulse);
        }
        else
        {
            Rb.AddForce (new Vector2 (-.2f, .3f) * speed * 7, ForceMode2D.Impulse);

        }
    }
開發者ID:Qlevine20,項目名稱:Demonology,代碼行數:13,代碼來源:StickImp.cs

示例8: Start

 // Use this for initialization
 void Start()
 {
     ball = GetComponent<Rigidbody2D>();
     //Randomly picks left or right side to go and add initial force to the ball
     float side = Random.Range(0.0f, 10.0f);
     if (side <= 5.0f)
     {
         ball.AddForce(new Vector2(horizontalForce * 50, 600));
     } else
     {
         ball.AddForce(new Vector2(-horizontalForce * 50, -600));
     }
 }
開發者ID:weikeatwk,項目名稱:Pong2D,代碼行數:14,代碼來源:ball_behavior.cs

示例9: Start

 // Use this for initialization
 void Start()
 {
     rb = GetComponent<Rigidbody2D> ();
     randomStart = Random.Range (1, 5);
     if (randomStart == 1) {
         rb.AddForce (new Vector2 (20f, 40f));
     } else if (randomStart == 2) {
         rb.AddForce (new Vector2 (-20f, 40f));
     } else if (randomStart == 3) {
         rb.AddForce (new Vector2 (20f, -40f));
     } else {
         rb.AddForce (new Vector2 (-20f, -40f));
     }
 }
開發者ID:Emiliandro,項目名稱:RangerVsPong,代碼行數:15,代碼來源:ballForce.cs

示例10: Start

	// Use this for initialization
	void Start () {
        player = GameObject.Find("Spaceman");
        PlayerController controller = player.GetComponent<PlayerController>();  

        physics = GetComponent<Rigidbody2D>();

        if (facingRight)
        {
            physics.AddForce(new Vector2(-20, 0), ForceMode2D.Impulse);
        }
        else
        {
            physics.AddForce(new Vector2(20, 0), ForceMode2D.Impulse);
        }
    }
開發者ID:opinnaytepaahto,項目名稱:Opinn-ytety-,代碼行數:16,代碼來源:BulletController.cs

示例11: timedPush

 public static EffectStop timedPush(Vector3 direction, Rigidbody2D target, DateTime castTime, int strikeTime, int forceAmount)
 {
     return () =>
     {
         target.AddForce(direction * forceAmount);
     };
 }
開發者ID:JD95,項目名稱:UnityScripts,代碼行數:7,代碼來源:PhysicsEffect2DPrimites.cs

示例12: fire

 public void fire(Vector2 position, Vector2 direction)
 {
     rd = GetComponent<Rigidbody2D>();
     rd.velocity = Vector2.zero;
     transform.position = position;
     rd.AddForce(direction.normalized * power);
 }
開發者ID:JackOfDawn,項目名稱:GameTech2,代碼行數:7,代碼來源:Projectile.cs

示例13: PlayerTwo

 public void PlayerTwo()
 {
     transform.position = new Vector3 (80, 0, 0);
     rb = GetComponent<Rigidbody2D> ();
     Vector2 initialForce = new Vector2 (Random.Range (.3f, .4f), Random.Range (-.05f, .05f));
     rb.AddForce(initialForce);
 }
開發者ID:mp3por,項目名稱:hackathon2015,代碼行數:7,代碼來源:BonusBrick.cs

示例14: AddExplosionForce

    public static void AddExplosionForce(Rigidbody2D body, float _explosionForce,
	                                     Vector3 explosionPosition, float _explosionRadius)
    {
        var dir = (body.transform.position - explosionPosition);
                float wearoff = 1 - (dir.magnitude / _explosionRadius);
                body.AddForce (dir.normalized * _explosionForce * wearoff);
    }
開發者ID:Jturesson90,項目名稱:MayThirteen,代碼行數:7,代碼來源:ExplodeScript.cs

示例15: Start

	void Start()
	{
		rg2d = GetComponent<Rigidbody2D> ();
		rg2d.position = new Vector2 (15, 13);
		Vector2 initCircling = new Vector2 (13, -15);
		rg2d.AddForce (initCircling, ForceMode2D.Impulse);
	}
開發者ID:xuechaow,項目名稱:Gravity-Hop,代碼行數:7,代碼來源:PlayerController.cs


注:本文中的UnityEngine.Rigidbody2D.AddForce方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。