AddForce() public method

Apply a force to the rigidbody.

public AddForce ( Vector2 force, [ mode ) : void
force Vector2 Components of the force in the X and Y axes.
mode [ The method used to apply the specified force.
return void
コード例 #1
0
ファイル: arroww2.cs プロジェクト: luliz/brazilball
    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);
    }
コード例 #2
0
ファイル: AttackController.cs プロジェクト: dunbarj/Firates
 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++;
         }
     }
 }
コード例 #3
0
ファイル: WallJump2D.cs プロジェクト: Dunmord/UnityMegamanX
    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;
                }
            }
        }
    }
コード例 #4
0
ファイル: Oscillate.cs プロジェクト: stults-p/Space-Runner
    // 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;
    }
コード例 #5
0
ファイル: Bullet.cs プロジェクト: vamidi/Project-Dimensia
    // 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);
    }
コード例 #6
0
ファイル: Bullet.cs プロジェクト: nebathemonk/UnityDemo
    // 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);
    }
コード例 #7
0
ファイル: ball_behavior.cs プロジェクト: weikeatwk/Pong2D
 // 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));
     }
 }
コード例 #8
0
ファイル: StickImp.cs プロジェクト: Qlevine20/Demonology
    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);

        }
    }
コード例 #9
0
ファイル: ballForce.cs プロジェクト: Emiliandro/RangerVsPong
 // 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));
     }
 }
コード例 #10
0
	// 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);
        }
    }
コード例 #11
0
    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);
    }
コード例 #12
0
    //[SerializeField]
    //[SerializeField] Sprite plutoHpPanel;
    // Use this for initialization
    void Start()
    {
        Destroy(gameObject, 3);

        rb = GetComponent<Rigidbody2D>();
        rb.AddForce(transform.right * bulletSpeed * 100);
    }
コード例 #13
0
 static public int AddForce(IntPtr l)
 {
     try{
         if (matchType(l, 2, typeof(UnityEngine.Vector2), typeof(UnityEngine.ForceMode2D)))
         {
             UnityEngine.Rigidbody2D self = (UnityEngine.Rigidbody2D)checkSelf(l);
             UnityEngine.Vector2     a1;
             checkType(l, 2, out a1);
             UnityEngine.ForceMode2D a2;
             checkEnum(l, 3, out a2);
             self.AddForce(a1, a2);
             return(0);
         }
         else if (matchType(l, 2, typeof(UnityEngine.Vector2)))
         {
             UnityEngine.Rigidbody2D self = (UnityEngine.Rigidbody2D)checkSelf(l);
             UnityEngine.Vector2     a1;
             checkType(l, 2, out a1);
             self.AddForce(a1);
             return(0);
         }
         LuaDLL.luaL_error(l, "No matched override function to call");
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
コード例 #14
0
 // Use this for initialization
 void Start()
 {
     rigidBody2D = GetComponent<Rigidbody2D>();
     dir = transform.rotation * Vector2.up;
     dir.Normalize();
     rigidBody2D.AddForce(dir * initialForce);
 }
コード例 #15
0
ファイル: Bomb.cs プロジェクト: TheBlackOrchid/Platformer2D
 void OnCollisionEnter2D(Collision2D coll)
 {
     Vector3 direction = (coll.transform.position - myTransform.position).normalized;
     playerRb = coll.gameObject.GetComponent<Rigidbody2D>();
     playerRb.AddForce(direction * boomForce * Time.fixedDeltaTime, ForceMode2D.Impulse);
     StartCoroutine(Explode());
 }
コード例 #16
0
 public static EffectStop timedPush(Vector3 direction, Rigidbody2D target, DateTime castTime, int strikeTime, int forceAmount)
 {
     return () =>
     {
         target.AddForce(direction * forceAmount);
     };
 }
コード例 #17
0
 void Start()
 {
     life = bulletLifetime;//life wor de totale levenstijd
     Rigidbody2D = GetComponent<Rigidbody2D>();//krijg de rigidbody van de bullet
     Rigidbody2D.velocity = GameObject.Find("Player").GetComponent<Rigidbody2D>().velocity;//maak de snelheid van de bullet even snel als die van de player
     Rigidbody2D.AddForce(transform.up * BulletForce);//set een kracht achter de bullet
 }
コード例 #18
0
    IEnumerator Shoot()
    {
        canShoot = false;

        audio.Play();
        currBullet = bulletPool.GetChild(currBulletIndex);

        currBulletRB = currBullet.GetComponent<Rigidbody2D>();
        currBulletBC = currBullet.GetComponent<BulletCleaner>();

        //currBullet.gameObject.layer = bulletLayer;
        currBullet.gameObject.SetActive(true);

        currBulletBC.origin = gameObject;
        currBullet.position = muzzle.position;
        currBullet.rotation = muzzle.rotation;
        currBulletRB.velocity = Vector2.zero;
        currBulletRB.angularVelocity = 0;
        currBulletRB.AddForce(muzzle.up * shootForce, ForceMode2D.Impulse);

        yield return shootRateWFS;
        canShoot = true;
        if (currBulletIndex < bulletPool.childCount - 1)
        {
            currBulletIndex++;
        }
        else
        {
            currBulletIndex = 0;
        }
    }
コード例 #19
0
 // Use this for initialization
 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
     movement = this.transform.position;
     movement = new Vector3(1f, 0, 1);
     rb.AddForce (movement * speed);
 }
コード例 #20
0
    // Use this for initialization
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();

        rigidBody.AddForce(new Vector2(Random.Range(-500f, 500f), 800f));
        rigidBody.angularVelocity = 500 * Random.Range(-1f, 1f);
    }
コード例 #21
0
ファイル: FireStaticShot.cs プロジェクト: MrHangover/GGJ2016
	// Use this for initialization
	void Start () {
        player = GameObject.FindGameObjectWithTag("Player");
        body = GetComponent<Rigidbody2D>();
        body.AddForce(new Vector2(player.transform.position.x - transform.position.x,
                      player.transform.position.y - transform.position.y).normalized * shotSpeed);
        Destroy(gameObject, 5f);
	}
コード例 #22
0
ファイル: MovingPlayer.cs プロジェクト: pompomyoyo/Twices
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        Vector2 movement = new Vector2 (0.0f,speed);

        rb.AddForce (movement);
    }
コード例 #23
0
ファイル: BonusBrick.cs プロジェクト: mp3por/hackathon2015
 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);
 }
コード例 #24
0
ファイル: Projectile.cs プロジェクト: JackOfDawn/GameTech2
 public void fire(Vector2 position, Vector2 direction)
 {
     rd = GetComponent<Rigidbody2D>();
     rd.velocity = Vector2.zero;
     transform.position = position;
     rd.AddForce(direction.normalized * power);
 }
コード例 #25
0
 // Update is called once per frame
 void Update()
 {
     if (cam.activePlayer == gameObject) {
         rb2D = GetComponent<Rigidbody2D>();
         if (isGrounded && Input.GetKeyDown ("space")) {
             rb2D.AddForce(Vector3.up * jumpSpeed);
         } else if (Input.GetKeyDown ("right")) {
             rb2D.AddForce(Vector3.right * moveSpeed);
         } else if (Input.GetKeyDown ("left")) {
             rb2D.AddForce(Vector3.left * moveSpeed);
         }
     }
     if (Input.GetKeyDown ("r")) {
         transform.position = new Vector3 (xPosInit, -3.6f, 0f);
     }
 }
コード例 #26
0
ファイル: Gravity.cs プロジェクト: TheOfficialSkara/Deadaleus
 private void Attract(Rigidbody2D a, Rigidbody2D b, double magnitude)
 {
     Vector2 forceDirection = b.worldCenterOfMass - a.worldCenterOfMass;
     forceDirection.Normalize();
     Vector2 force = forceDirection * (float)magnitude;
     a.AddForce(force);
 }
コード例 #27
0
	void Start()
	{
		rg2d = GetComponent<Rigidbody2D> ();
		rg2d.position = new Vector2 (15, 13);
		Vector2 initCircling = new Vector2 (13, -15);
		rg2d.AddForce (initCircling, ForceMode2D.Impulse);
	}
コード例 #28
0
	void Init(){
		Player = GameObject.FindGameObjectWithTag("Player");
		direction = Player.GetComponent<Player_Control>().VecDirection;
		rb = gameObject.GetComponent<Rigidbody2D>();
		rb.AddForce(direction*moveSpeed);

	}
コード例 #29
0
 static public int AddForce(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (argc == 2)
         {
             UnityEngine.Rigidbody2D self = (UnityEngine.Rigidbody2D)checkSelf(l);
             UnityEngine.Vector2     a1;
             checkType(l, 2, out a1);
             self.AddForce(a1);
             pushValue(l, true);
             return(1);
         }
         else if (argc == 3)
         {
             UnityEngine.Rigidbody2D self = (UnityEngine.Rigidbody2D)checkSelf(l);
             UnityEngine.Vector2     a1;
             checkType(l, 2, out a1);
             UnityEngine.ForceMode2D a2;
             checkEnum(l, 3, out a2);
             self.AddForce(a1, a2);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function AddForce to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
コード例 #30
0
ファイル: PallaDiFuoco.cs プロジェクト: Eibis/GGJ2016
    void Start()
    {

        rb = GetComponent<Rigidbody2D>();

        if (direction_d)
        {
            rb.AddForce(transform.right * 40, ForceMode2D.Impulse);
        }
        else
        {
            rb.AddForce(transform.right * -40, ForceMode2D.Impulse);

        }

    }
コード例 #31
0
	// Use this for initialization
	void Start ()
	{

        rb = GetComponent<Rigidbody2D>();
        rb.AddForce(new Vector2(600.0f, 1100.0f));
        StartCoroutine(WaitAndDie(5.0F));
    }
コード例 #32
0
 // All moving objects call this function to make sure they aren't moving too fast
 // This is a hack to prevent extremely fast objects from achieving escape velocity and breaking beyond the game's barriers
 public static void AddRetardingForce(Rigidbody2D extremelyFastObject)
 {
     if(extremelyFastObject.velocity.sqrMagnitude > SwipeballConstants.PhysicsHacks.SquareSpeedCap)
     {
         extremelyFastObject.AddForce((extremelyFastObject.velocity.magnitude - SwipeballConstants.PhysicsHacks.SpeedCap) * - extremelyFastObject.velocity.normalized);
     }
 }
コード例 #33
0
 public override void OnFixedUpdate()
 {
     if (rigidbody != null)
     {
         rigidbody.AddForce(owner.GetValue(force));
     }
     Finish();
 }
コード例 #34
0
ファイル: AddForce.cs プロジェクト: warboy123/goap
        public override TaskStatus OnUpdate()
        {
            if (targetRigidbody2D == null)
            {
                Debug.LogWarning("Rigidbody2D is null");
                return(TaskStatus.Failure);
            }

            targetRigidbody2D.AddForce(force.Value);

            return(TaskStatus.Success);
        }
コード例 #35
0
 public void throttleUp()
 {
     UnityEngine.Rigidbody2D rb = getRigidBody2d();
     rb.AddForce(Vector2.up * upwardForceToApply);
 }