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 Cold_Ankles · Sep 09, 2012 at 03:10 AM · rotationrigidbodyinstantiatepositionprojectile

Objects Instantiating at wrong position

When I call:

 function fireRocket(){
 
     var rocketclone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation);
     rocketclone.velocity = transform.TransformDirection (Vector3.forward * 300);
     Destroy(rocketclone.gameObject,10);
 
 }

It instantiates the rocket at position (0,0,0) with rotation (0,0,0), however it returns the position as that of the player (at whose position it is supposed to instantiate) when Debug.Log(rocketclone.position) is called.

Help!

FYI:

The script is attached to the player.

function fireRocket() is called from Update thus:

 if (Input.GetButtonDown("Fire2")){
 
     //shoot rocket;
     fireRocket();
 
 }



Entire Script:

 //Inspector Variables
 
 var maxSpeed    : float = 80.0;
 var minSpeed    : float = 40.0;
 var armor        : float = 100.0;
 var health        : float = 100.0;
 var curSpeed    : float = 50.0;
 var turnSpeed    : float = 10.0;
 var planecam    : GameObject;
 var bullet        : Rigidbody;
 var fireSpeed    : float = 0.05;
 var rocket        : Rigidbody;
 var ammoType    : String;
 var rocketAmmo    : int = 8;
 var controlNum    : int = 360;
 var damage        : float;
 
 //Private Variables
 private var turn : float;
 
 function Start(){
 
     InvokeRepeating("fireGuns",0.0,fireSpeed);
 
 }
 
 function Update(){
 
     turn = Input.GetAxis("Horizontal")*turnSpeed;
     
     curSpeed = curSpeed + (Input.GetAxis("Speed")*0.2);    
     
     if(curSpeed<minSpeed){
     
         curSpeed=minSpeed;
     
     }
     
     if(curSpeed>maxSpeed){
     
         curSpeed=maxSpeed;
     
     }
 
     if (Input.GetButtonDown("Roll")){
 
         //execute aileron roll function;
         controlNum = 0;
 
     }
 
     if (Input.GetButtonDown("Fire2")){
 
         //shoot rocket;
         fireRocket();
 
     }
 
     if (Input.GetButtonDown("Special")){
 
         //execute special function;
 
     }
 
     movePlane();
     rollFunc();
     
     if(damage!=0){
     
         calcDmg();
     
     }
 
 }
 
 function movePlane(){
 
     if(controlNum>=360){
 
         transform.Rotate(0,turn,0);
         
     }
     transform.Translate((Vector3(0,0,1)*curSpeed)/2,Space.Self);
     
     planecam.transform.position.x = transform.position.x;
     planecam.transform.position.z = transform.position.z;
 }
 
 function fireGuns(){
 
 if (Input.GetButton("Fire1")){
 
         var localOffset=Vector3(20,0,0);
         var worldOffset1 = transform.rotation * localOffset;
         var worldOffset2 = transform.rotation * -localOffset;
         var cannon1 = transform.position + worldOffset1;
         var cannon2 = transform.position + worldOffset2;
 
         var clone : Rigidbody = Instantiate(bullet,cannon1,transform.rotation);
         var clone2 : Rigidbody = Instantiate(bullet,cannon2,transform.rotation);
         clone.velocity = transform.TransformDirection (Vector3.forward * (400 + (curSpeed*curSpeed)));
         clone2.velocity = transform.TransformDirection (Vector3.forward * (400 + (curSpeed*curSpeed)));
         
         Physics.IgnoreCollision(clone.collider, collider);
         Physics.IgnoreCollision(clone2.collider, collider);
         
         Destroy(clone.gameObject,5);
         Destroy(clone2.gameObject,5);
         
         }
 }
 
 function fireRocket(){
 
     if(rocketAmmo>0){
         var rocketclone : Rigidbody = Instantiate(rocket,transform.position,transform.rotation);
         rocketclone.velocity = transform.forward * 200;
         var rscript : rocketScript = rocketclone.GetComponent("rocketScript");
         rscript.type = ammoType;
         Physics.IgnoreCollision(rocketclone.collider, collider);
         rocketAmmo = rocketAmmo - 1;
     }
 }
 
 function rollFunc(){
 
     if(controlNum<=360){
     
         collider.isTrigger = true;
         transform.Rotate(0,0,5,Space.Self);
         controlNum = controlNum + 5;
     
     }
     else{
     
         collider.isTrigger = false;
     
     }
 
 }
 
 function OnCollisionEnter(collision : Collision){
 
     
 
 }
 
 function calcDmg(){
     
     var adam = damage/armor;
     var hdam = damage - adam;
     
     armor = armor - adam;
     health = health - hdam;
     
     damage = 0;
     
 }
Comment
Add comment · Show 2
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 Latedi · Sep 09, 2012 at 05:21 AM 0
Share

Just here to say I have the exact same problem, I think. The script with the instantiate function is attached to the player but the object instantiates where the player was spawned, not where they're currently at.

avatar image Cold_Ankles · Sep 09, 2012 at 05:41 AM 0
Share

$$anonymous$$ine is unconnected to the spawn of the player. It instantiates at (0,0,0) wherever the player spawns.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by botelho_gt · May 02, 2017 at 01:23 PM

Alright... long time later, but...

I have a script instantiating enemies e random positions, but i did something that was making my enemies being instantiated in wrong positions when i pressed play on the game. I found out that deactivating the Animator in the enemy inspector made the instantiation get correct again. Somehow its bugging the instantiation position...

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 Eric5h5 · Sep 09, 2012 at 03:47 AM

It's using the transform.position and transform.rotation of whatever object the script is attached to. If that's not what you want, you need to reference a different transform. (By the way, you can just do transform.forward instead of transform.TransformDirection (Vector3.forward). )

Comment
Add comment · Show 5 · 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 Cold_Ankles · Sep 09, 2012 at 04:01 AM 0
Share

The script is attached to the player so transform.position / transform.rotation are that of the player (which is what I want), however it still appears at (0,0,0).

Thanks for the transform.forward thing!

avatar image Eric5h5 · Sep 09, 2012 at 06:38 AM 0
Share

You have something overriding it then.

avatar image Cold_Ankles · Sep 09, 2012 at 09:35 AM 0
Share

I can't see anything that could be overriding it though. It's the only place I work on the rocket's transform.

avatar image whydoidoit · Sep 09, 2012 at 10:01 AM 0
Share

The rocket itself doesn't have a script attached?

avatar image Cold_Ankles · Sep 09, 2012 at 11:45 AM 0
Share

It does:

//Inspector Variables var type : String;

//Private Variables

function OnCollisionEnter(collision : Collision){

if(collision.gameObject.tag == "enemy"){ }

}

avatar image
0

Answer by ArtOfWarfare · Oct 07, 2013 at 10:57 PM

Do you have an extra object in your scene that happens to also be spawning the rocket that shouldn't be?

I had the exact same symptoms as you and that ended up being my issue.

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

13 People are following this question.

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

Related Questions

Fire Projectile based on Model rotation 1 Answer

Spawn a bullet of a cannon at right position 1 Answer

transform position and rotation of instantiated object 1 Answer

Unfreeze Rigidbody ROTATION only 1 Answer

How can I Instantiate a prefab (projectile) consistently from the character? 0 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