Hi, how can i rotate an object in specific axis for example just rotate it left or right. Normally i just make vector y axis same as objects axis that will be rotated but just changing rotation of x and z axis should work but it doesnt work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class tankFire : MonoBehaviour
{
public float soundVolume;
public AudioClip fireSound;
public AudioSource mySource;
public float projectileSpeed;
public float projectileSize;
public GameObject fireSmoke;
public GameObject projectile;
public GameObject fireFire;
public Transform turretPivot;
public Transform gunPivot;
public Transform projectileLoc;
public float baseTimer;
public float timer;
public float turretRotationRange;
public Transform enemy;
Quaternion targetRotation;
Vector3 targetDirection;
void Start()
{
mySettings.effectVolume = 1f;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.K)) fire();
rotateTurret();
}
void rotateTurret()
{
targetDirection = enemy.position - gunPivot.position;
targetRotation = Quaternion.LookRotation(targetDirection);
Quaternion gunRotation = targetRotation;
gunRotation = Quaternion.Euler(0f, gunRotation.eulerAngles.y, 0f);
float angle = Quaternion.Angle(transform.rotation, targetRotation);
timer += Time.deltaTime;
//if (angle > turretRotationRange) return;
gunPivot.rotation = Quaternion.Lerp(gunPivot.rotation, gunRotation, 5f * Time.deltaTime);
}
void calculateTimer()
{
timer += Time.deltaTime;
if (timer >= baseTimer)
{
fire();
timer = 0f;
}
}
void fire()
{
mySource.PlayOneShot(fireSound, soundVolume * mySettings.effectVolume);
GameObject currentBullet = Instantiate(projectile, projectileLoc.position, targetRotation);
currentBullet.transform.localScale *= projectileSize;
currentBullet.GetComponent<Rigidbody>().velocity = targetDirection.normalized * projectileSpeed;
Instantiate(fireSmoke, projectileLoc.position, targetRotation);
}
}