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 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' which acts as a pivot point and the parent of the 'gun' and the 'barrel'. Everything 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 this 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
         while (mousePos2.y > 90)
         {
 
 
             barrel.transform.position = new Vector3(barrel.transform.position.x, barrel.transform.position.y, 0f);
         }
 
 
 
     }
 }
Comment
Add comment · 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 ins$$anonymous$$d 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.

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

Zooming camera to child objects of .obj model 0 Answers

How do I get the global position 2 Answers

Behave like another object is parent? 2 Answers

How can I make a game object follow an instantiated game object? 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