当前位置: 首页>>代码示例>>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;未经允许,请勿转载。