• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How does Rigidbody.MovePosition works ? What's behind the API? 0 Answers

Smooth Player Ball Rolling 1 Answer

Why is my vertical movement faster than my horizontal? 1 Answer

Rigidbody keeps sliding 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges