- Home /
Making a projectile specific model/mesh/etc rather than a primitive
Hey everyone, I just started using Unity yesterday so please overlook my ignorance over the next few weeks :)
Basically, I have the following code that, after pressing the mouse, shoots a projectile from the camera to the mouse click's location (ignore the "print" commands, they're just bug testing) :
function Start () {
}
function Update () {
if (Input.GetButtonDown ("Fire1")) {
print("foobar");
var ray : Ray = camera.ScreenPointToRay (Input.mousePosition);
print(ray);
var sphere : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
print(sphere);
sphere.transform.position = ray.origin;
sphere.AddComponent("Rigidbody");
sphere.transform.localScale += Vector3(.5, .5, .5);
sphere.rigidbody.AddForce (ray.direction * 2000);
}
}
As it stands, I create a primitive sphere that shoots out and collides with various items. What I want, however, is to use a custom-made model (an arrow). After importing my arrow into Unity how do I change the shot object from being a sphere to being that arrow?
Answer by justinpatterson · Aug 14, 2012 at 09:11 PM
The answer to this issue is as follows:
In the beginning of the script where you want to instantiate a prefab, write: var prefabClone : GameObject;
The part of the script where you want to instantiate that prefab clone, write: var arrow = Instantiate(prefabClone, transform.position, transform.rotation );
Attach the script to something. In my case, since I wanted to have an arrow shoot from my camera, so I attached it to my camera.
Open the GameObject with the attached script in the hierarchy tab. Look at its inspector on the right pane. Notice that if you expand the script area (in my case, the "shooter" script that I attached, you see an empty gameObject that is the variable you defined at the beginning of the script (in my case, "prefabClone").
Assuming you have the prefab titled "prefabClone" already created in the Project tab, drag it from that tab to the inspector, on top of the empty GameObject in the script you created.
Congrats! You're done! The only issue with my code is the local rotation - if the camera moves, the projectile still tries to orient to the previous camera viewing angle (if I turn around in-game, the arrow will appear to be oriented backwards as it fires forward).
My code:
var arrowshoot : GameObject;
/ one, two, skip a few methods /
function Update () {
if (Input.GetButtonDown ("Fire1")) {
var ray : Ray = camera.ScreenPointToRay (Input.mousePosition);
print(ray); var sphere = Instantiate(arrowshoot, transform.position, transform.rotation ); sphere.transform.position = ray.origin; sphere.transform.localScale += Vector3(.5, .5, .5); sphere.transform.localRotation.x = 1; sphere.rigidbody.AddForce (ray.direction * 5000); } }
Answer by fafase · Aug 10, 2012 at 09:05 PM
Instantiation is what you are looking for:
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
BTW, this line
var sphere : GameObject = GameObject.print(sphere);
looks weird to me. You declare a sphere var and right after you use that.
Answer by justinpatterson · Aug 11, 2012 at 08:11 AM
whoops, accidentally pasted the same line twice. I edited it. Thanks for the correction. I'll look into Instantiation.
Answer by Ingen · Aug 10, 2012 at 09:22 PM
- put your arrow in the scene, add it rigidbody,
in asset create new prefab add it your arrow , drag from hierarchies, then delete arrow from scene/hierarchies
if fly bad remember the z axis is forward
if have bad collision remove mesh collider and add a capsule collider
http://docs.unity3d.com/Documentation/Manual/AssetWorkflow.html
http://docs.unity3d.com/Documentation/Manual/Prefabs.html
then use the prefab you have create from arrow as bullet remember to give rigidbody at the arrow
Your answer
Follow this Question
Related Questions
[Solved]Shoot a projectile towards a target location. 3 Answers
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers
Shooting projectiles once per second. 1 Answer
Projectile Shooting Problem 1 Answer
For some akward reason bullet is moving in the wrong direction 1 Answer