public AddForce ( |
||
force | Components of the force in the X and Y axes. | |
mode | [ | The method used to apply the specified force. |
return | void |
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); }
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++; } } }
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; } } } }
// 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; }
// 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); }
// 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); }
// 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)); } }
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); } }
// 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)); } }
// 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); } }
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); }
//[SerializeField] //[SerializeField] Sprite plutoHpPanel; // Use this for initialization void Start() { Destroy(gameObject, 3); rb = GetComponent<Rigidbody2D>(); rb.AddForce(transform.right * bulletSpeed * 100); }
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); } }
// Use this for initialization void Start() { rigidBody2D = GetComponent<Rigidbody2D>(); dir = transform.rotation * Vector2.up; dir.Normalize(); rigidBody2D.AddForce(dir * initialForce); }
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()); }
public static EffectStop timedPush(Vector3 direction, Rigidbody2D target, DateTime castTime, int strikeTime, int forceAmount) { return () => { target.AddForce(direction * forceAmount); }; }
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 }
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; } }
// Use this for initialization void Start() { rb = GetComponent<Rigidbody2D>(); movement = this.transform.position; movement = new Vector3(1f, 0, 1); rb.AddForce (movement * speed); }
// 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); }
// 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); }
void Start() { rb = GetComponent<Rigidbody2D>(); Vector2 movement = new Vector2 (0.0f,speed); rb.AddForce (movement); }
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); }
public void fire(Vector2 position, Vector2 direction) { rd = GetComponent<Rigidbody2D>(); rd.velocity = Vector2.zero; transform.position = position; rd.AddForce(direction.normalized * power); }
// 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); } }
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); }
void Start() { rg2d = GetComponent<Rigidbody2D> (); rg2d.position = new Vector2 (15, 13); Vector2 initCircling = new Vector2 (13, -15); rg2d.AddForce (initCircling, ForceMode2D.Impulse); }
void Init(){ Player = GameObject.FindGameObjectWithTag("Player"); direction = Player.GetComponent<Player_Control>().VecDirection; rb = gameObject.GetComponent<Rigidbody2D>(); rb.AddForce(direction*moveSpeed); }
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)); } }
void Start() { rb = GetComponent<Rigidbody2D>(); if (direction_d) { rb.AddForce(transform.right * 40, ForceMode2D.Impulse); } else { rb.AddForce(transform.right * -40, ForceMode2D.Impulse); } }
// Use this for initialization void Start () { rb = GetComponent<Rigidbody2D>(); rb.AddForce(new Vector2(600.0f, 1100.0f)); StartCoroutine(WaitAndDie(5.0F)); }
// 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); } }
public override void OnFixedUpdate() { if (rigidbody != null) { rigidbody.AddForce(owner.GetValue(force)); } Finish(); }
public override TaskStatus OnUpdate() { if (targetRigidbody2D == null) { Debug.LogWarning("Rigidbody2D is null"); return(TaskStatus.Failure); } targetRigidbody2D.AddForce(force.Value); return(TaskStatus.Success); }
public void throttleUp() { UnityEngine.Rigidbody2D rb = getRigidBody2d(); rb.AddForce(Vector2.up * upwardForceToApply); }