- Home /
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:
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:
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:
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.
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:
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.