Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 carter-carl30 · May 17, 2013 at 02:05 PM · rigidbodyraycasttouchaddforceraycasthit

raycast hit add force problem, help needed please

I have this current script (see below), what i'm trying to do is have a wall (made up of seperate blocks) and when you touch a block it adds force and falls down with gravity.

In my scene I just have "castle_brick" prefabs which at the moment are just cubes with rigidbody attached, duplicated to form a wall.

What is happening at the moment is when I touch any brick it plays the sound multiple times and all the bricks fly away together (as though one touch on any brick is hitting them all at once)

any help in what i'm doing wrong would be appriciated, cheers :)

 private var ray : Ray;
 private var rayCastHit : RaycastHit;
 var mySound: AudioClip;
 var bloodPrefab : GameObject;
 var direction = transform.forward;
 var force = 2000;
 
 function Update(){
 
 if(Input.GetMouseButtonDown(0)){
 
 ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
 if (Physics.Raycast (ray, rayCastHit)){
 
 //add name of object/button to affect in ""
 if(rayCastHit.transform.name == "castle_brick"){
 
 //play sound
 audio.clip = mySound; 
 audio.PlayOneShot(mySound);
 
 Debug.Log("Score!");
 Scorecounter.Counter +=5;
 
 //create blood prefab
 var rot = Quaternion.FromToRotation(Vector3.up, rayCastHit.normal);
 Instantiate(bloodPrefab, rayCastHit.point, rot);
 
 //add force to object
 rigidbody.AddForceAtPosition(force * direction, rayCastHit.point);
 
 Debug.Log ("hit castle_brick!");
 }
 }
 }
 }
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 MD_Reptile · May 17, 2013 at 02:22 PM 0
Share

what gameobject(s) is/are this script attached to?

1 Reply

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

Answer by robertbu · May 17, 2013 at 02:20 PM

What object do you have this script attached to? Line 31 implies that you have this attached to the bricks themselves. Is that true or do you have it attached to another rigidbody?

It "should" be attached to another game object (not the bricks). You don't want a raycast for every brick. Then on like 31 you would apply the force to whatever was hit:

 hit.rigidbody.AddForceAtPosition(force * direction, rayCastHit.point);

As for 'direction' you may want to experiment with using the negated normal of the hit.

Note you are getting multiple hits because your Raycast() returns true for any brick, not just the brick this script is attached to. So every brick is thinking it is hit, and every brick is playing the audio, and every brick is adding force.

Comment
Add comment · Show 4 · 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 carter-carl30 · May 17, 2013 at 02:36 PM 0
Share

I have the script attached to an empty gameobject called "script_manager".

when I add the line hit.rigidbody.AddForceAtPosition(force * direction, rayCastHit.point);

I get "unknown identifier: 'hit'. I changed it to rayCastHit.rigidbody.AddForceAtPosition(force * direction, rayCastHit.point);

this hit and added the force to individual bricks but it still played the sound for all of them.

Do I need to name every brick with a different name?

avatar image carter-carl30 · May 17, 2013 at 02:50 PM 0
Share

I got it working now, I had the script attached to some of the bricks also hehe thankyou very much for helping me out! :)

avatar image robertbu · May 17, 2013 at 03:10 PM 0
Share

Sorry about the 'hit'. As you figure out it should have been rayCastHit.

avatar image carter-carl30 · May 17, 2013 at 03:16 PM 0
Share

no problem :) thanks for helping me get it working!

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

fire to camera 1 Answer

Move toward Mouse Cursor (RaycastHit), without following? 1 Answer

Casting a ray at touch position 1 Answer

Raycasting not adding force to rigidbody 1 Answer

RaycastHit behind object 2 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