- Home /
Throwing Objects
Ok so I want to throw objects randomly on a board - So they all land in random places.
I have attempted a script but it is a little to embarrassing as I'm not the best programmer yet.
Any help will be very helpful. Just looking for a starting script that I can play with.
Thanks
var projectile : Transform;
var projectile2 : Transform;
function Update () {
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
var clone2 : Transform;
var clone3 : Transform;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone2 = Instantiate(projectile2, transform.position, transform.rotation);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * 400);
clone2.rigidbody.AddForce(clone.transform.forward * 400);
}
}
Even "embarrassing" code is often gets you better answers than no code because it educates the person attempting to answer your question about what you are trying to do. "Throw" can mean several things depending on how you are moving your objects for example. If you were simply translating them the answer would be different than if you are using a Rigidbody and adding force.
robertblu and Julien.Lynge are absolutely correct. Refer to this video : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
I keep having these mood swings, currently my mood is set to 'mercy'. Search Unity Gravity Gun : http://lmgtfy.com/?q=Unity+Gravity+Gun
Answer by Julien-Lynge · Mar 20, 2013 at 07:15 PM
Your post (as it appears now) isn't something we can give you a quick answer for, and isn't really appropriate for UnityAnswers. The UnityAnswers philosophy is:
"[Unity Answers] is a place to ask specific questions that have specific answers. The forum is a better place to post discussions and non-technical questions."
It sounds like what you're looking for is Unity training, rather than a specific answer. I would suggest visiting the following training websites to find the one that best helps you move forward. In addition to the sites below, you can always search YouTube, which has a large number of user-created Unity tutorials.
3DBuzz (hover over the Unity dropdown) - http://www.3dbuzz.com/vbforum/sv_home.php
Lynda - https://www.lynda.com/
BurgZergArcade - http://www.burgzergarcade.com/
Unity3DStudent - http://www.unity3dstudent.com/
UnityGems - http://unitygems.com/
CGCookie - http://cgcookie.com/unity/
Answer by Finn_C · Aug 13, 2021 at 02:08 AM
There is absolutely no way you're still working on the same issue, but this is for anyone who needs to see this. You could try adding a force to the rigidbody component of whatever object you want to be thrown. For example, if a certain button is pressed, a force would be added to the rigidbody causing it to move forward. using UnityEngine;
public class ThrowingObject : MonoBehaviour {
public Rigidbody rigidBody;
public float forwardForce = 2000f;
void FixedUpdate ()
{
if (Input.GetKey("Fire1"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
}
}
}
This could work.