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 karl-police · Jun 14, 2019 at 11:25 AM · 2drotationtransformvector3parent and child

How to rotate a child where its parent has another Z rotation? | Issue when rotating towards the mouse after it goes on the other side of the player

I am trying to rotate all these Pivots for the gun barrels at the position where the mouse is:

alt text

Just that I run into a problem, because the 2 pivots on the left, basically GunHolder_3 and GunHolder_4 has the child GunBarrel_Pivot.

This is the tree of the whole thing:

alt text

So basically the pivots that have GunHolder_1 and GunHolder_2 as a parent, rotate fine.

The one pivots on the left GunHolder_3 and GunHolder_4 are not working correctly, when basically, when the mouse starts moving on the left of the player.

After trying to find out why, I found out that x and y was swapping and so I changed both rotation Z to this, so no swapping happens. This didn't fix it correctly though. Also, I have to change the Z again, so I'm trying to find a solution for each z.

This is the GunHolder_3:

alt text

As you can see, it has its Z set to 180, which I need it to be, so I can do other stuff with it.

The two on the right have the GunHolder Z set to 0, that's why it is working fine.

At the same time, I'm trying to clamp the angles, I'm defining an minimum and a maximum for each Pivot.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController_Tank : MonoBehaviour
 {
     public Rigidbody2D rb;
     public GameObject PlayerTank;
 
     private Vector3 mousePos;
 
     public float moveSpeed = 5f;
     public float speedMultiplicator = 1f;
 
     public GameObject GunHolder_1;
     public GameObject GunHolder_2;
     public GameObject GunHolder_3;
     public GameObject GunHolder_4;
 
     private GameObject GunBarrel_1_Pivot;
     private GameObject GunBarrel_2_Pivot;
     private GameObject GunBarrel_3_Pivot;
     private GameObject GunBarrel_4_Pivot;
 
     void GetGunHolders()
     {
         if (GunHolder_1 != null) { GunBarrel_1_Pivot = GunHolder_1.transform.GetChild(0).gameObject; }
         if (GunHolder_2 != null) { GunBarrel_2_Pivot = GunHolder_2.transform.GetChild(0).gameObject; }
         if (GunHolder_3 != null) { GunBarrel_3_Pivot = GunHolder_3.transform.GetChild(0).gameObject; }
         if (GunHolder_4 != null) { GunBarrel_4_Pivot = GunHolder_4.transform.GetChild(0).gameObject; }
     }
 
 
     // Start is called before the first frame update
     void Start()
     {
         GetGunHolders();
     }
 
     // Update is called once per frame
     void Update()
     {
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
 
         Vector3 tempVect = new Vector3(h, v, 0);
         tempVect = tempVect.normalized * (moveSpeed * Time.deltaTime) * speedMultiplicator;
         rb.MovePosition(rb.transform.position + tempVect);
 
 
         float AngleRad = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x - transform.position.x);
         float angle = (180 / Mathf.PI) * AngleRad;
 
         float angle1 = Mathf.Clamp(angle, -62, 126);
         float angle2 = Mathf.Clamp(angle, -126, 62);
         float angle3 = Mathf.Clamp(angle, -162, 62);
         float angle4 = Mathf.Clamp(angle, 18, 118);
 
         GunBarrel_1_Pivot.transform.rotation = Quaternion.AngleAxis(angle1, Vector3.forward);
         GunBarrel_2_Pivot.transform.rotation = Quaternion.AngleAxis(angle2, Vector3.forward);
         GunBarrel_3_Pivot.transform.rotation = Quaternion.AngleAxis(angle3, Vector3.forward);
         GunBarrel_4_Pivot.transform.rotation = Quaternion.AngleAxis(angle4, Vector3.forward);
     }
 }


Here is a Pastebin version: https://pastebin.com/HEE4qF2X

So this is the code and the angles for each Clamp should be correct right.

This would be -62 for the Pivot that has GunHolder_3 as the parent.

alt text

I'm trying to make each GunBarrel_Pivot to look at the mouse position I'm currently at, but the angles have to be clamped. I've tried various threads I've seen that localRotation is a thing but I couldn't fix it correctly because each time when I did, the ones on the left stopped following when I move my mouse to my right, after checking if the mouse is left or right of the player.

This is what is happening at the moment:

alt text

The reason why float angle4 = Mathf.Clamp(angle, 18, 118); has different angles as the others, is because of the Z Rotation having 180 and somehow setting it to 18 makes it have -62 or something, the problem is that even doing it this method won't fix it, because the Z Rotation resets when it hits 0 and changes direction or something.

Comparing float angle3 = Mathf.Clamp(angle, -162, 62); it's not doing what it should do, comparing the image above.

This is the thread that I found but I wasn't really able to fix it, https://answers.unity.com/questions/1126515/2d-child-object-does-not-rotate-like-parent-does.html

How can I fix that for any kind of Z Rotation. Let's say I have a child that has a parent where its Z rotation is set to something else and I want the child which is the pivot to look at the mouse without any issues.

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

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

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

Moving by rotation of objects 0 Answers

Simultaneously translating and rotating 2D sprite? 1 Answer

Player not facing the mouse correctly 1 Answer

Using xbox controller with top down shooter 0 Answers

Rotate towards target position (mouse) at a limited speed 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