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 13, 2015 at 10:48 AM · movementphysicsrigidbodytouchexplosion

Touch input to move rigidbody using explosion force in C#

I am really new to unity and coding so I have been picking up snippets of code from other places and trying to put it together to make it work for my project.

I'm trying to set it up so that when the user touches a point on his screen, an explosion occurs and throws a 3D cube in the opposite direction. I created a 3D cube object and set it as a rigidbody and a larger 3D rectangle to act as the floor/ground (I did not set the larger rectangle as a rigidbody). Both of these objects have the box colliders that automatically come with the objects when you create them. 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 and AddExplosionForce to try and move the cube. 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)
      {
          // Get current touch location
          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));
          
          // Explosion Effect
          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 1
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 saud_ahmed020 · Mar 13, 2015 at 12:18 PM 0
Share

Are you sure both bodies have colliders?? If not then attach respective colliders to bodies.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Move rigid body 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

Collisions causing Rigidbody to go Haywire and move randomly. 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