Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 em2 · Aug 31, 2011 at 09:13 AM · camerarotationweaponclone

Irregular position when instantiating clone during camera rotation?

I have a weapon dummy object sitting in front of my main camera (fps). This dummy is a child of the main camera so it is intended that where the camera moves / rotates the dummy will correspond. Using an array which is filled with a number of weapon prefabs, I am instantiating a particular weapon to the dummy object during weapon switching (i.e. when I switch weapons the previous clone is destroyed and the new one cloned to the dummy object. I'm using the first person controls from mobile assets for rotating the camera.

The problem I'm having is that when I'm switching my weapons, the weapon object seems to be lagging slightly and is showing up in a slightly different position each time based on the current rotation direction of the camera. What I mean is, if I'm moving the camera at the same time as switching weapons, this is effecting where the weapon clone is showing up. I'm not sure what code to put up to get help with the problem as I'm not sure exactly what is causing the problem. Only thing I can think of is it is lagging when instantiating. Here is my code to instantiate the weapon and ensure it follows the dummy object. As you can see I'm setting the vector3's and rotation and on Update are making sure the clone follows the parent.

 public var myWeaponsArray:GameObject[];
 public static var chosenWeapon:String;
 public var clone:GameObject;
 
 //******************
 //sets the default weapon to the item in position zero of array
 function Start(){
     chosenWeapon = "Weapon1";
     clone = Instantiate(myWeaponsArray[0], Vector3(transform.position.x,transform.position.y,transform.position.z), transform.rotation);
 }
 
 private var currentWeapon:String="";
 //******************
 function LoadWeapon (chosenWeapon) {
     if (currentWeapon!=chosenWeapon){
         switch (chosenWeapon){
             case ("Weapon1"):
             //instantiate bow in here
                 Destroy(clone);
                 clone = Instantiate(myWeaponsArray[0], Vector3(transform.position.x,transform.position.y,transform.position.z), transform.rotation);
                 currentWeapon = "Weapon1";
                 break;
             case ("Weapon2"):    
                 Destroy(clone);
                 clone = Instantiate(myWeaponsArray[1], Vector3(transform.position.x,transform.position.y,transform.position.z), transform.rotation);
                 currentWeapon = "Weapon2";
                 break;
         }    
       }
 }
 //******************
 //make the clone follow the camera
 function Update (){
     //keeps updated the orientation of the weapon object to the parent object(main camera)
         clone.transform.parent = gameObject.transform;
 }




In summary, my problem is the slight moving of my weapon to different positions when switching between weapons and then back again (i.e from weapon 1 to 2 and back to 1), whilst rotating the camera.

Any advice would be really appreciated. Cheers Chris

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

1 Reply

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

Answer by Waz · Aug 31, 2011 at 10:41 AM

Most likely you are calling LoadWeapon from some function that executes after the Update function, hence it is misplaced by a frame. Then, your Update function is setting the parent, which is not at all what you should be doing.

Just set the parent once, when you create the weapon, not every frame, and the problem should go away.

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 em2 · Sep 01, 2011 at 12:53 AM 0
Share

Worked great, just by moving the parenting to inside the instantitaion case. Thanks Warwick. Cheers Chris

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera X and Y velocity HELP! 0 Answers

how do i make first person character rotate left and right along with camera? 0 Answers

Override Oculus Orientation 5.3 1 Answer

How to make the camera rotate only when right click is held down on the mouse? 1 Answer

Turning a character to match camera rotation 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