Adding a constant force to a barrel when i shoot it (some kind of high pressure that is eliberated)

its like this: i have a barrel and when i shoot it with raycast bullets i whant to add a constant force to that spot of the barrel where i shooted untill explodes need some help or script example maybe..Thanx

You are going to want to use the RaycastHit class to determine which Rigidbody was hit and some variation of the Rigidbody.AddForce() to that.

var power = 15;
var radius = 5;
var upwardsPush = 4;

function Update () {
    var hit : RaycastHit;

    if(Physics.Raycast(someRay, hit, someDistance)) {
        hit.rigidbody.AddExplosionForce(power,hit.point, radius, upwardPush);
        //or
        //var forceDir : Vector3 = someRay.direction;
        //hit.rigidbody.AddForceAtPosition(forceDir * power, hit.point);
    }
}
//Raycast is unfinished, you need to add your values there.

I achieved this a little differently. It works perfectly. I shoot a propane tank, and particles emit from the tank to look like pressure / fire. Also, a constant force is added to hit.point. I did this by making a prefab, which consists of a rigidbody, and a particle emitter. I instantiate the prefab, and add a constant relative force, and a FixedJoint. The connected body to the FixedJoint is set as hit.rigidbody, a.k.a. the tank you are shooting. You must set a clone variable for it to work correctly, so that one “pressure prefab” doesn’t try to use multiple tanks at once as a connected body. You must also tag the “Tank or Barrel” as “Pressurized” for this specific code to work. This allows the pressure only to be instantiated on specific objects, all other rigidbodies will simply receive an explosion force in one frame.

Here is a piece of code I came up with to do all of this :slight_smile:

var pressure : Rigidbody;

var pressure_force : float;

function Update()

{

FireWeapon();

}

var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;

if (Physics.Raycast (transform.position, direction, hit, 1000))

{

	

	if(Input.GetMouseButtonDown(0))

	{

		hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);

if(hit.rigidbody)

		{

			

			if(hit.rigidbody.gameObject.tag=="Pressurized")

			{

				var clone : Rigidbody;

				clone = Instantiate(pressure, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));

				clone.gameObject.AddComponent("ConstantForce");

				clone.constantForce.relativeForce = new Vector3 (0, 0, -pressure_force);

				clone.gameObject.AddComponent(FixedJoint).connectedBody = hit.rigidbody;

			

			}

			else

			{

				hit.rigidbody.AddExplosionForce(100, hit.point, 1000);

			}

		}

	}

}

I achieved this a little differently. It works perfectly. I shoot a propane tank, and particles emit from the tank to look like pressure / fire. Also, a constant force is added to hit.point. I did this by making a prefab, which consists of a rigidbody, and a particle emitter. I instantiate the prefab, and add a constant relative force, and a FixedJoint. The connected body to the FixedJoint is set as hit.rigidbody, a.k.a. the tank you are shooting. You must set a clone variable for it to work correctly, so that one “pressure prefab” doesn’t try to use multiple tanks at once as a connected body. You must also tag the “Tank or Barrel” as “Pressurized” for this specific code to work. This allows the pressure only to be instantiated on specific objects, all other rigidbodies will simply receive an explosion force in one frame.

Here is a piece of code I came up with to do all of this :slight_smile:

var pressure : Rigidbody;

var pressure_force : float;

function Update()

{

FireWeapon();

}

var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;

if (Physics.Raycast (transform.position, direction, hit, 1000))

{

	

	if(Input.GetMouseButtonDown(0))

	{

		hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);

if(hit.rigidbody)

		{

			

			if(hit.rigidbody.gameObject.tag=="Pressurized")

			{

				var clone : Rigidbody;

				clone = Instantiate(pressure, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));

				clone.gameObject.AddComponent("ConstantForce");

				clone.constantForce.relativeForce = new Vector3 (0, 0, -pressure_force);

				clone.gameObject.AddComponent(FixedJoint).connectedBody = hit.rigidbody;

			

			}

			else

			{

				hit.rigidbody.AddExplosionForce(100, hit.point, 1000);

			}

		}

	}

}