Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by i ConnE · Jul 17, 2010 at 09:41 AM · gameobjectshootaddconstantforce

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Peter G · Jul 17, 2010 at 10:33 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image i ConnE · Jul 17, 2010 at 11:29 AM 0
Share

I dont know anithing about scripting.. what specifical values?

avatar image Peter G · Jul 17, 2010 at 02:58 PM 0
Share

By raycast is unfinished, I mean that the script above will not actually compile because the Physics.Raycast is made up. You said you already have the raycast bullets made up, so rather than spend extra time adding that to this post, I posted the Raycast as psuedo-code because the only part you need is the AddForce... part

avatar image
0

Answer by Clunk · Sep 16, 2011 at 05:42 AM

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 :)

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);

             }

         }

     }

 }

 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Clunk · Sep 16, 2011 at 05:42 AM

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 :)

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);

             }

         }

     }

 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Constantforce in user interface. 1 Answer

add GameObject in GameObject[] (js) 2 Answers

Filling up an array with GameObjects 0 Answers

Adds infinite # of components, using GameObject.AddComponent. 4 Answers

Problems creating new List of type GameObject and adding GameObject to it. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges