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 Seth-Bergman · Mar 30, 2011 at 05:54 AM · instantiatetransformpositionprojectile

How can I Instantiate a prefab (projectile) consistently from the character?

Basically, I have an enemy which periodically turns toward the player and fires a projectile or two. To be safe, I created an empty game object, childed to the enemy, and instance the projectiles from that.I've managed to get this working perfectly on a character I modeled. However, now I must rebuild it on a different model. Problem is, with this new model (which someone else made), half the time the projectile appears way below the floor. They still move in the right direction, but I can't figure out how to get the projectile to instance consistently from the character. I tried creating a cube and attaching that to the enemy,with the projectile script attached to that, so I could see if the object itself is moving, and the cube remains consistently at chest level to the enemy, even as the projectiles instantiate way below it.

var ogreProjectilePrefab : Rigidbody; var throwForce : int = 100;

function Release () { var projectile : Rigidbody = Instantiate (ogreProjectilePrefab, transform.position, transform.rotation); projectile.velocity = transform.TransformDirection(Vector3 (0, 0, throwForce)); //projectile.AddForce(projectile.transform.forward * throwForce);

using AddForce has the same result. What am I missing? I've also tried a different way:

var ogreProjectilePrefab : Rigidbody; var throwForce : int = 100; var target : Transform; function Release () { var player = GameObject.FindWithTag("Player");

  target = player.transform;
   direction = transform.position - target.transform.position  ;
         //direction.y= 0;
     var Projectile : Rigidbody = Instantiate (ogreProjectilePrefab, transform.position, transform.rotation);
     Projectile.velocity = transform.TransformDirection(direction);

     //Physics.IgnoreCollision(Projectile.collider, transform.root.collider); //This keeps the collider on the projectile from messing with the collider on the player.

}

Still has the same problem, plus the projectiles shoot off sideways to the right, nowhere near the direction of the player. Please Help!

Comment
Add comment · Show 1
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 Benproductions1 · Mar 21, 2013 at 08:42 AM 0
Share

Ok, for you second piece of code, you are already calculating the direction in world space, meaning you don't have to call transform.TransformDirection on your direction, ins$$anonymous$$d just set your velocity to direction :) Also please promise me you don't plan on calling FindWithTag in any proper project :P Just the idea of forcing Unity to go through all objects in a scene just to find one, when it can be easily directly set in every situation, makes shivers run down my spine :)

0 Replies

· Add your reply
  • Sort: 

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

1 Person is following this question.

avatar image

Related Questions

How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer

How can I get a GameObject's transform in game and set that to a variable? 1 Answer

Use GameObject's global position instead of local position 3 Answers

How do I get the opposite position of an object that spins around another object... 0 Answers

Objects Instantiating at wrong position 3 Answers


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