- Home /
Question by
Daniloleemes · Aug 01, 2020 at 01:20 PM ·
rotationvector33d models
Transform.Rotate not working as expected
Hi all, I'm trying to rotate 4 turrets but one of them is not behaving accordingly. Here's what is happening (notice how the bottom right one is different)
I tried debugging and it seems that via code the Rotation can't have negative values and thus my logic kinda breaks, because in the bottom left case I'd like it to rotate from 0 to -90 on Y axis. How could I achieve that?
Here's my code:
void FixedUpdate() {
float currentRotationY = cannonBase.transform.eulerAngles.y * (cannonBase.transform.rotation.y < 0 ? -1 : 1);
switch (currentPosition) {
case WeaponPosition.BOTTOM_LEFT:
RotateCannon(currentRotationY);
break;
case WeaponPosition.TOP_LEFT:
RotateCannon(currentRotationY - 90);
break;
case WeaponPosition.TOP_RIGHT:
RotateCannon(currentRotationY - 180);
break;
case WeaponPosition.BOTTOM_RIGHT:
RotateCannon(currentRotationY);
break;
default:
break;
}
}
private void RotateCannon(float currentRotationY) {
int maxCannonAngle = 90;
int minCannonAngle = 0;
if (currentRotationY <= minCannonAngle) animationDirection = AnimationDirection.FORWARD;
if (currentRotationY >= maxCannonAngle) animationDirection = AnimationDirection.BACKWARD;
if (currentRotationY < maxCannonAngle && animationDirection == AnimationDirection.FORWARD) {
cannonBase.transform.Rotate(Vector3.up * (rotationSpeed * Time.deltaTime));
} else if (currentRotationY > minCannonAngle && animationDirection == AnimationDirection.BACKWARD) {
cannonBase.transform.Rotate(Vector3.down * (rotationSpeed * Time.deltaTime));
}
}
And this is how I spawn the turrets and terrain:
private int[] possibleRotations = new int[4] { 0, 90, 180, 360 };
private int[] weaponRotations = new int[4] { 0, 180, 0, 180 };
private List<GameObject> weapons = new List<GameObject>();
// Start is called before the first frame update
void Start() {
instance = this;
var whichWeapon = weaponPrefabs[Random.Range(0, weaponPrefabs.Count)];
for (int x = 0; x < gridSize; x++) {
for (int z = 0; z < gridSize; z++) {
GameObject floorTile;
if (x == 0 && z == 0 || x == 0 && z == gridSize - 1 || x == gridSize - 1 && z == gridSize - 1 || x == gridSize - 1 && z == 0) {
var weapon = Instantiate(whichWeapon, gameObject.transform);
floorTile = Instantiate(cornerFloorPrefab, gameObject.transform);
weapon.transform.Translate(new Vector3(x, 0.5f, z));
weapons.Add(weapon);
weapon.transform.Rotate(new Vector3(0, weaponRotations[weapons.Count-1], 0));
} else {
floorTile = Instantiate(floorPrefabs[Random.Range(0, floorPrefabs.Count - 1)], gameObject.transform);
}
floorTile.transform.Translate(new Vector3(x, 0, z));
floorTile.transform.Rotate(new Vector3(0, possibleRotations[Random.Range(0, possibleRotations.Length - 1)], 0));
}
}
for (int x = 0; x < gridSize; x++) {
for (int z = 0; z < gridSize; z++) {
var floorTile = Instantiate(floorPrefabs[Random.Range(0, floorPrefabs.Count)], gameObject.transform);
floorTile.transform.Translate(new Vector3(x, -1, z));
}
}
}
Comment
Your answer