本文整理汇总了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++;
}
}
}
示例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;
}
}
}
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}
}
示例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));
}
}
示例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));
}
}
示例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);
}
}
示例11: timedPush
public static EffectStop timedPush(Vector3 direction, Rigidbody2D target, DateTime castTime, int strikeTime, int forceAmount)
{
return () =>
{
target.AddForce(direction * forceAmount);
};
}
示例12: fire
public void fire(Vector2 position, Vector2 direction)
{
rd = GetComponent<Rigidbody2D>();
rd.velocity = Vector2.zero;
transform.position = position;
rd.AddForce(direction.normalized * power);
}
示例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);
}
示例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);
}
示例15: Start
void Start()
{
rg2d = GetComponent<Rigidbody2D> ();
rg2d.position = new Vector2 (15, 13);
Vector2 initCircling = new Vector2 (13, -15);
rg2d.AddForce (initCircling, ForceMode2D.Impulse);
}