- Home /
How To Throw anything ?
Hey everybody, This sound very Silly but i'm working on a new game and i cant Programming or Modelling I need to throw a object called "Hard drive" I just want to pick it up and Throw it can someone give me a script please im already looking for a script for already 5 days or more.
yes it is very silly. In 5 days you could have learnt to script it yourself, your unlikely to get anyone to just write you a script and honestly you really haven't got a chance at making a game if you cant model or write script
if you're working on a game and you can't program or model... what are you doing?
i can talk,speak,write in english Very Good ,my vocabulary isnt just the best
wow I admit i was wrong, someone actually did write you a script! also its not your english thats so much of a problem its the fact that you need to learn some scripting language, I would suggest unityscript first as there are a lot of resources and its quite easy to pickup in a short amount of time. I'm not the best modeler for sure but I have learn't enough on blender to make basic things or alter resources I find :) good luck
Hey, it's a good practice for me as well, and if someone is a little happier with it around, why not? :)
Answer by asafsitner · Sep 15, 2011 at 07:20 PM
You might want to make an inventory for the player, then upon pickup (collision probably) add the prefab to the inventory and destroy it. Then upon throwing it (probably a key press) you'll instantiate the prefab and remove it from the inventory. But really, making games isn't as easy as drag-n'-drop, even with tools such as GameMaker or FPSCreator. If you want to make games you need to know game-making-related things, such as scripting (and really there's no reason you can't learn JavaScript at the very least).
Example script (c#):
using UnityEngine;
using System.Collections;
public class ThrowTest : MonoBehaviour
{
public Transform Inventory;
public float ThrowDistance = 2f;
void OnCollisionEnter (Collision collision)
{
if ( collision.transform.tag == "pickup" )
{
Inventory = collision.transform;
Destroy(collision.transform.gameObject);
}
}
void Update()
{
if (Input.GetKeyDown("space"))
{
var thrownItem = Instantiate(Inventory, transform.position, Quaternion.identity) as Transform;
thrownItem.name = Inventory.name;
thrownItem.Translate(0, 0, ThrowDistance);
Inventory = null;
}
}
}
no ,i dont want to get an inventory just to pick up something and to throw it,i already learning javascript but i think this can take a while can you make a second code for me please ?
The inventory is just a temporary container for whatever you pick up. But I think you mean something else, like the player carrying the object while it's still visible on screen? (possibly half-transparent like in deus-ex)
Answer by misterxxx · Sep 16, 2011 at 11:30 AM
I know its stupid but i can modelling just not so good and i will learn javascript now i just need to find something in german
No, you need to learn english, while you're at it. That way, you'll be able to benefit from A LOT more of the examples you find online, AND you won't have to listen to german voices dubbed on top of hollywood movies. :-D
Answer by asafsitner · Sep 16, 2011 at 12:33 PM
To make this one work you'll have to manually add the HoldPosition game object to the player's hierarchy and position it where you want the hands to be.
using UnityEngine;
using System.Collections;
public class ThrowTest : MonoBehaviour
{
public Transform HoldPosition; //This is the point where the "hands" of the player would be
public float ThrowForce = 10f; //How strong the throw is. This assumes the picked object has a rigidbody component attached
public float AlphaAmount = 0.5f; //this will be the alpha amount. It's public so you can change it in the editor
private Transform _pickedObject;
private Color _originalColor;
void OnCollisionEnter (Collision collision)
{
if ( collision.transform.tag == "pickup" )
{
//caches the picked object
_pickedObject = collision.transform;
//caches the picked object's color for resetting later
_originalColor = _pickedObject.renderer.material.color;
//this will snap the picked object to the "hands" of the player
_pickedObject.position = HoldPosition.position;
//this will set the HoldPosition as the parent of the pickup so it will stay there
_pickedObject.parent = HoldPosition;
//this will change the alpha amount on the object's color to make it half transparent
_pickedObject.renderer.material.color = new Color(_pickedObject.renderer.material.color.r,
_pickedObject.renderer.material.color.g,
_pickedObject.renderer.material.color.b,
AlphaAmount);
}
}
void Update()
{
if (Input.GetKeyDown("space"))
{
if (_pickedObject != null)
{
//resets the pickup's parent to null so it won't keep following the player
_pickedObject.parent = null;
//resets the pickup's color so it won't stay half transparent forever
_pickedObject.renderer.material.color = _originalColor;
//applies force to the rigidbody to create a throw
_pickedObject.rigidbody.AddRelativeForce(transform.forward * ThrowForce, ForceMode.Impulse);
//resets the _pickedObject
_pickedObject = null;
}
}
}
}
Forgot to add, but you'll want the pickup object to be rendered on a different layer, like you'd do with weapons. http://unity3d.com/support/documentation/$$anonymous$$anual/Cameras.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612213504im_/https://answers.unity.com/themes/thub/images/avi.jpg)