- Home /
Throwing an object on input
I am making a game where the player combines chemicals to make compounds which they use to complete challenges. I have written a code for the combining of the chemicals. I have two global variables, "water" and "HP"(Hydrogen peroxide), both made of Hydrogen and Oxygen. I have a problem with one of my scripts- the script that allows the player to "throw" the vial of chemicals. The input of a key is suppose to send the chemical vial (with rigidbody) flying in the direction the player is facing (first person). I need variables like "speed/power" and "Rigidbody" (for what the vial mesh is). I am decent with JavaScript, but know nothing about C#. I attempted this code, but I cannot figure out the vector () or any other force related commands. Can someone please try and explain either the Vector () command with examples, How I might approach this problem, or just explain to me how they would do the code?
This is URGENT!!!! I need it done by Sunday march 15th 2013.
also, I don't just want code. If you do give me code, can you explain what you did so I can learn from it?
I am really clueless to how to start this! I have read the Unity page on the Vector command.
-Thanks in advance
Your throwing is just another projectile script. There are a bizillion projectile and shooter scripts on UnityAnswers. If this is first person, you likely looking for one that shoots from the center of the screen. Google this list for any combination of projectile, shoot, bullet, gun, center of screen. Read and play. Find a simple one and implement in a simple project. There are lots of beginner questions and answers. Pay attention to how to create prefabs, how to link a prefab with a script, how to attach a Rigidbody, transform.LookAt(), and how to add force to a rigid body.
What robertbu said is correct. You also may want to look into what vectors are as they play a large role in 3D development.
Answer by amphoterik · Mar 13, 2013 at 03:27 AM
Assuming you have a "player" that will be the source of the vial, you could do something like what's posted below. This will create a vial at the player position (centered) and throw it along the z axis with a force of 1 (vial's mass will determine how effective this is). This was all typed in here so there may be some typos, but that is the gist.
if(Input.GetButtonDown("Fire1")){
var temp = Instantiate(nameOfVialPrefab, new Vector3(transform.position.x, transform.position.y, transform.position.z), transform.rotation);
temp.rigidbody.AddForce(0, 0, 1f);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613094313im_/https://answers.unity.com/themes/thub/images/avi.jpg)