AddForceAtPosition() public method

Apply a force at a given position in space.

public AddForceAtPosition ( Vector2 force, Vector2 position, [ mode ) : void
force Vector2 Components of the force in the X and Y axes.
position Vector2 Position in world space to apply the force.
mode [ The method used to apply the specified force.
return void
コード例 #1
0
 static public int AddForceAtPosition(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (argc == 3)
         {
             UnityEngine.Rigidbody2D self = (UnityEngine.Rigidbody2D)checkSelf(l);
             UnityEngine.Vector2     a1;
             checkType(l, 2, out a1);
             UnityEngine.Vector2 a2;
             checkType(l, 3, out a2);
             self.AddForceAtPosition(a1, a2);
             return(0);
         }
         else if (argc == 4)
         {
             UnityEngine.Rigidbody2D self = (UnityEngine.Rigidbody2D)checkSelf(l);
             UnityEngine.Vector2     a1;
             checkType(l, 2, out a1);
             UnityEngine.Vector2 a2;
             checkType(l, 3, out a2);
             UnityEngine.ForceMode2D a3;
             checkEnum(l, 4, out a3);
             self.AddForceAtPosition(a1, a2, a3);
             return(0);
         }
         return(error(l, "No matched override function to call"));
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
コード例 #2
0
 static public int AddForceAtPosition(IntPtr l)
 {
     try{
         if (matchType(l, 2, typeof(UnityEngine.Vector2), typeof(UnityEngine.Vector2), typeof(UnityEngine.ForceMode2D)))
         {
             UnityEngine.Rigidbody2D self = (UnityEngine.Rigidbody2D)checkSelf(l);
             UnityEngine.Vector2     a1;
             checkType(l, 2, out a1);
             UnityEngine.Vector2 a2;
             checkType(l, 3, out a2);
             UnityEngine.ForceMode2D a3;
             checkEnum(l, 4, out a3);
             self.AddForceAtPosition(a1, a2, a3);
             return(0);
         }
         else if (matchType(l, 2, typeof(UnityEngine.Vector2), typeof(UnityEngine.Vector2)))
         {
             UnityEngine.Rigidbody2D self = (UnityEngine.Rigidbody2D)checkSelf(l);
             UnityEngine.Vector2     a1;
             checkType(l, 2, out a1);
             UnityEngine.Vector2 a2;
             checkType(l, 3, out a2);
             self.AddForceAtPosition(a1, a2);
             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);
     }
 }
コード例 #3
0
        public override TaskStatus OnUpdate()
        {
            if (targetRigidbody2D == null)
            {
                Debug.LogWarning("Rigidbody2D is null");
                return(TaskStatus.Failure);
            }

            targetRigidbody2D.AddForceAtPosition(force.Value, position.Value);

            return(TaskStatus.Success);
        }
	void Update()
	{
        //Checks for Arduino Serial Communication Port
		if(!serial.IsOpen)
		{
			serial.Open();
		}

        //Parse the string of two bits into a value to initiate shooting or a faster run.
		string data = serial.ReadLine();
		int shoot = int.Parse(data.Substring (0, 1));
		int fast = int.Parse(data.Substring (1, 1));
		
		if (shoot == 1) 
		{
			isShooting = true;
		} 
		else 
		{
			isShooting = false;
		}
		
		if (fast == 1) 
		{
			isRunning = true;
		} 
		else 
		{
			isRunning = false;
		}
		

		if (isShooting) 
		{
			rocketIns = Instantiate (rocket, this.transform.position, this.transform.rotation) as Rigidbody2D;
			rocketIns.AddForceAtPosition (new Vector2 (2000, 0), this.transform.position);
		} 
		if (isRunning) 
		{
			this.transform.parent.gameObject.GetComponent<Controller2DCustom> ().setSpeed (60);
		} 
		else 
		{
			this.transform.parent.gameObject.GetComponent<Controller2DCustom>().setSpeed(30);
		}
	}
コード例 #5
0
    private void Explode(Rigidbody2D target)
    {
        explosion_obj.Explode();

        // push target
        if (target != null)
        {
            Vector2 to_target = target.transform.position - transform.position;
            Vector2 force = to_target.normalized * Mathf.Max(0, (3f - to_target.magnitude));
            target.AddForceAtPosition(force, transform.position, ForceMode2D.Impulse);
        }

        // particles
        trail.Clear();
        trail.enableEmission = false;

        exploded = true;
    }
コード例 #6
0
	private void AddForceToRigidGivenVelo(Rigidbody2D target, Rigidbody2D fromRigid, ContactPoint2D atPosition){
		Vector2 force = new Vector2(0.5f * fromRigid.mass * Mathf.Pow(fromRigid.velocity.x,2f), 
									0.5f * fromRigid.mass * Mathf.Pow(fromRigid.velocity.y,2f));
		
		target.AddForceAtPosition(force, atPosition.point);
	}
コード例 #7
0
    // Use this for initialization
    void Start()
    {
        Vector3 position = transform.position;

        if (positionOfImpulse != null) {
            position = positionOfImpulse.position;
        }

        r2d = GetComponent<Rigidbody2D> ();
        r2d.AddForceAtPosition (Vector2.up.Rotate (Random.Range (minYAngle, maxYAngle)) * Random.Range (minImpulse, maxImpulse), position, ForceMode2D.Impulse);

        minStopAngle = minStopAngle;
        maxStopAngle = maxStopAngle;

        if (minStopAngle > maxStopAngle) {
            float t = minStopAngle;
            minStopAngle = maxStopAngle;
            maxStopAngle = t;
        }

        startY = transform.position.y;
    }
コード例 #8
0
ファイル: explosionForce.cs プロジェクト: slehmann1/purgatory
    private void addForce(Rigidbody2D rigBody)
    {
		RaycastHit2D rh = Physics2D.Linecast (transform.position, rigBody.transform.position);
		float force = (addForce (rh.distance));
        float angle = getAngle(transform.position,rigBody.transform.position);
        Vector2 forceVector = new Vector2(-Mathf.Cos(angle),-Mathf.Sin(angle));
        forceVector *= force;
		Debug.DrawRay (rigBody.transform.position,forceVector.normalized,Color.cyan,5f,false);
		rigBody.AddForceAtPosition (forceVector,rh.point);
        if(rigBody.GetComponent<Snappable>()){
            if (rigBody.GetComponent<Snappable>().snapOnExplode)
            {
                rigBody.GetComponent<Snappable>().destroy();
            }
        }
        if (rigBody.GetComponent<explosionForce>())
        {
            explosionForce e = rigBody.GetComponent<explosionForce>();
            if (e.explodeWithExplosion)
            {
                StartCoroutine(exp(e));
                
            }
        }
    }