• 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
Question by aronatvw · Jul 22, 2014 at 04:11 AM · instantiatepositionparentchildlookat

Problem with Parent and Child using LookAt

I am making a 2D platformer gunner. The gun aims at the mouse position. I have an invisible 'arm' w$$anonymous$$ch acts as a pivot point and the parent of the 'gun' and the 'barrel'. Everyt$$anonymous$$ng works fine except...

I need the barrel to stay at 0 because that is where the bullets get instantiated from. The barrel stays in place when aiming to the right of my character, but when aiming to the left the barrel.transform.postion.z goes from 0 to -1.5.

Also in the inspector it shows the arms y rotation just jumping back and forth between 90° and 270°.

 using UnityEngine;
 using System.Collections;
 
 public class GunContLookAt : MonoBehaviour {
 
     public GameObject barrel;
     public GameObject arm;
     public Vector3 pubMousePos;
 
 
     // Use t$$anonymous$$s for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         Vector3 mousePos2 = new Vector3(mousePos.x,mousePos.y, arm.transform.position.z);
 
         pubMousePos = mousePos2;
 
         arm.transform.LookAt(mousePos2);
 
         // Test doesnt work
         w$$anonymous$$le (mousePos2.y > 90)
         {
 
 
             barrel.transform.position = new Vector3(barrel.transform.position.x, barrel.transform.position.y, 0f);
         }
 
 
 
     }
 }
Comment

People who like this

0 Show 4
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 andrew-lukasik · Jul 22, 2014 at 07:57 AM 0
Share

Hi there. First thing to notice: Don't use while(){} because it runs everything {/here/} in one frame. That mean in your case it just freezes your computer.

avatar image sethuraj · Jul 22, 2014 at 11:03 AM 0
Share

If you are trying to lock the position using while loop,try If condition instead of while loop

 //Test doesn't work
 if(mousePos2.y > 90)
 {
   barrel.transform.position = new Vector3(barrel.transform.position.x, barrel.transform.position.y, 0f);        
 }
avatar image VioKyma · Jul 22, 2014 at 11:11 AM 0
Share

If the barrel has a rigid body, you can select the constraint to freeze the Z axis for transform and/or rotation. If this is not the case, @sethuraj has provided a good option above to hold the barrel in place through code.

avatar image aronatvw · Jul 22, 2014 at 04:36 PM 0
Share

I tried the if statement, it didnt work which is why I tried a while loop which didnt work.

I have also tried restricting the rotation of z and y on all components of the gun and it doesnt do anything, lookAt overrides it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 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 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

Make a simple tree 1 Answer

How to find and remove the last child of parent? 1 Answer

Prefab, procedural attachment of child, childCount return 0 1 Answer

Rotating a parent-child pair 2 Answers

Create Clones as children in loops? 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