Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by justinpatterson · Aug 10, 2012 at 09:01 PM · modelshootingprojectile

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by justinpatterson · Aug 14, 2012 at 09:11 PM

The answer to this issue is as follows:

  1. In the beginning of the script where you want to instantiate a prefab, write: var prefabClone : GameObject;

  2. The part of the script where you want to instantiate that prefab clone, write: var arrow = Instantiate(prefabClone, transform.position, transform.rotation );

  3. 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.

  4. 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").

  5. 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.

  6. 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); } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges