• 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 Christopher K. · Jan 07, 2011 at 01:50 PM · instantiateenemyprojectile

Instantiating in a position relative to an object.

Hey, I'm trying to make a ship's missile appear 5 units from the ship but from the ship's perspective, not the world coordinates. As is, the missile appears at the ship's world z-coordinate plus 5, so at certain angles the missile collides with the ship.

My code so far:

function Fire(){
    if (transform.position.x >= -140 && transform.position.x <= 140 && transform.position.z <= 134 && transform.position.z >= -95) {
        audio.Play();
        Instantiate(projectile, Vector3(transform.position.x, transform.position.y, transform.position.z + 5), transform.rotation);
        gameObject.SendMessage ("Shoot", SendMessageOptions.DontRequireReceiver);
        }
    state = State.MOVING;
}

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

2 Replies

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

Answer by Damyan · Jan 07, 2011 at 01:59 PM

You need to convert your offset from the local coordinate system (ie relative to your ship) to the world's coordinate system. You can do this by applying the transform's rotation to your offset:

var localOffset = Vector3(0,0,5);
var worldOffset = transform.rotation * localOffset;
var spawnPosition = transform.position + worldOffset;

Comment
Add comment · Show 4 · 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 Christopher K. · Jan 07, 2011 at 02:18 PM 0
Share

Thanks! It worked perfectly!

avatar image Zeeshan Anjum · Apr 07, 2014 at 10:40 PM 0
Share

Thanks! :)

avatar image Amyr · Sep 20, 2014 at 03:09 PM 0
Share

How do I make it in C#?

avatar image loliconest · Jan 09, 2015 at 09:07 AM 0
Share

hi man, can you write your code in c#? thank you so much!

avatar image
1

Answer by Amyr · Jan 09, 2015 at 12:28 PM

Vector3 localOffset = new Vector3(0,0,5);

Vector3 worldOffset = transform.rotation * localOffset;

Vector3 spawnPosition = transform.position + worldOffset;

But I do this in a different way. I create empty objects and make them child of my ship. I put this empty game objects in the position/orientation I wish the fire to spawn. Since they are children of the ship, they will rotate and translate accordingly, whenever you move the ship. I call those objects (Transforms) cannonPosition1, cannonPosition2, etc.

Then I simply instantiate like this:

Instantiate(projectile, cannonPosition.position, cannonPosition.rotation);

Notice that this way you can also make the cannon shoot in directions that are different from the ships orientation. Its useful if you want to make an intelligent cannon that aims by itself. You only have to properly control the cannonPosition transforms. Remember to make them child of the ship, so they navigate properly with the ship.

Comment
Add comment · Show 1 · 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 launchpad.network · Jan 15, 2016 at 04:24 AM 0
Share

The empty object trick worked great for me - you can mount any number of handles onto an object, use them as references - and edit their position and rotation visually in the editor.

         Camera.main.transform.position = Handle.transform.position;
         Camera.main.transform.rotation = Handle.transform.rotation;

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObject.Find - Need help... 1 Answer

Error: Instantiated Enemies don't get hit 2 Answers

Radial/omni directional shooting Problem 1 Answer

Enemy Instantiating one bullet 3 Answers

Multiplayer Projectile Instantiation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges