Bartek Dusza
Bartek Dusza

Reputation: 174

Tank barrel rotation

Hello i'm making a game in unity about tanks and currently i'm making a script about the turret of a tank and I've come to a problem with the tank gun. When i'm rotating the tower the gun still stays in the same position facing the same way all the time

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankManager: MonoBehaviour
{
    public GameObject turret;
    public GameObject gun;
    float mouseX=0;
    float mouseY=-90;
    public float moveSpeed;
    private void FixedUpdate()
    {
        MoveTower();
    }
    void MoveTower()
    {
        mouseX += Input.GetAxis("Mouse X");
        mouseY += Input.GetAxis("Mouse Y")*-1;
        turret.transform.rotation = Quaternion.RotateTowards(turret.transform.rotation, Quaternion.Euler(new Vector4(-90, mouseX, -180)), moveSpeed);
        gun.transform.rotation = Quaternion.RotateTowards(gun.transform.rotation, Quaternion.Euler(new Vector3(mouseY, 0, turret.transform.rotation.z)), moveSpeed);

    }
}

Hope you can help me.

Upvotes: 2

Views: 574

Answers (1)

Frenchy
Frenchy

Reputation: 17007

hum if i guess the problem without any other precision, you should test the localRotation for the gun

gun.transform.localrotation = .....

rotation is in worldspace, localrotation is for rotation gun relative the turret

Upvotes: 2

Related Questions