Quantcast
Channel: Answers by "Flynn"
Viewing all articles
Browse latest Browse all 59

Answer by Flynn

$
0
0
That is a very broad question you asked there. It's more of a "How do I go about programming this" question than "What is the issue HERE" question. You'd have better luck with answers on the forum I think. To *try* and answer your question, first, you need to detect clicks on the sphere. There are two ways of doing this: First is OnMouseDOwn on a script inside the sphere. This is your best choice right now, as it's very simple to implement I think. [http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html][1] [http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html][2] The other way is with raycasting. Sample code: C#: //This generates a prebuilt ray in the direction the mouse is effectively pointing. Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0)); //This prepares a new hit object. This object will contain the data sent back by our ray. RaycastHit hit = new RaycastHit(); //This calls Physics.Raycast on the ray and hit we made. IF the hit succeeds it will return true, making the if activate. if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity)) { //hit.transform is now the transform of what we hit. You can use if (hit.transform == ourObject.transform) to see if we hit the object. } JS: //This generates a prebuilt ray in the direction the mouse is effectively pointing. var ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0)); //This prepares a new hit object. This object will contain the data sent back by our ray. var hit = new RaycastHit(); //This calls Physics.Raycast on the ray and hit we made. IF the hit succeeds it will return true, making the if activate. if (Physics.Raycast(ray.origin, ray.direction, out hit, Mathf.Infinity)) { //hit.transform is now the transform of what we hit. You can use if (hit.transform == ourObject.transform) to see if we hit the object. } **Untested code, may have a few bugs here and there Once you can detect that the object has been clicked, you can use a rigidbody to add force. Using the OnClick method: This gives us no way to determine where it was clicked, so, We'll just shake it to the global right. This is the only downside of OnMouseDown. this.rigidbody.AddForce (Vector3.right * 10); // Vector3's can be multiplied by normal numbers. This multiplies all 3 values in the vector by 10. The raycast method: This actually tells us where the hit occurred, allowing us to do force accordingly. We can make an explosion happen at the click point. hit.rigidbody.AddExplosionForce(10, hit.point, 7, 0);//Adds force with power ten, radius 7, upwards force 0 at hit.point to hit.rigidbody. The rigidbody will take care of slowing down and halting for you. [1]: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html [2]: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html

Viewing all articles
Browse latest Browse all 59

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>