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 toon1334 · Mar 10, 2015 at 09:34 AM · movementphysicsrigidbodytouch controlsexplosion

Move rigid body using explosion force in C#

I am really new to unity and coding so I might be going about this the wrong way but I'm trying to set it up so that when the user touches a point on his screen, an explosion occurs and throws a box (onscreen rigidbody gameobject) in the opposite direction. I am wanting the force to occur at the point where the user touches the screen and the farther the touch and is from the object, the weaker the force. I am using Overlap.Sphere for detection and AddExplosionForce to add force to the box. Right now, nothing is happening when I touch the screen. (I am also wanting the box to move only in x,y axis, but I dont know how to do that, or even if that is possible) I am sure that there are packages out there that can do this, but I am really wanting to learn how to code and to figure/understand why my code isnt working.

Since I am very very new, if there is something wrong with my code, could you please provide the correct code that I would need to use since Im not sure if I would be able to figure out how to implement them into my code on my own. Any help would be greatly appreciated.

 public class PlayerMovement : MonoBehaviour {
     public float radius = 10.0F;
     public float power = 10.0F;
 
     // Use this for initialization
     void Start () {
     
     }
     
     void Update () {
     for (int i = 0; i < Input.touchCount; i++)
     {
         Touch touch = Input.GetTouch(i);

         // -- Tap: quick touch & release
         if (touch.phase == TouchPhase.Ended && touch.tapCount == 1)
         {
             // Touch are screens location. Convert to world
             Vector2 touchPos = Input.GetTouch(i).position;

             // convert touch's screen position to world coordinates
             // z position is 10, camera is set to -10
             Vector3 worldPos = Camera.main.ScreenToWorldPoint(new Vector3(touchPos.x, touchPos.y, 10));
             
             // Effect for feedback
             Collider[] colliders = Physics.OverlapSphere(worldPos, radius);
             foreach (Collider hit in colliders) {
                 if (hit.attachedRigidbody)
                     hit.attachedRigidbody.AddExplosionForce(power, worldPos, radius, 3.0F);
                 
             }
         }
     }
 }
Comment
Add comment · Show 2
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 bobin115 · Mar 10, 2015 at 10:18 AM 0
Share

there is a free package on the asset store which does exactly what you are asking it is called "true explosions"

avatar image hexagonius · Mar 10, 2015 at 04:57 PM 0
Share

Usually the camera is at -10 on the z-Axis to the actual game, which happens on z == 0. When transfor$$anonymous$$g the touch.position into the world, it is at the near clip plane of the cam, far away from the game. set the touch positions z to negative camera.position.z before transfor$$anonymous$$g it into the scene

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Touch input to move rigidbody using explosion force in C# 0 Answers

RigidBody immediately stops after AddForce 1 Answer

Unity Physics On Input Issue? 0 Answers

Strange Rigidbody Behavior 0 Answers

Set Rigidbody.velocity.x with Touch? All devices same speed. 0 Answers


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