Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question how to rotate object in specific axis

Discussion in 'Scripting' started by kader1081, May 17, 2024.

  1. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    401
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class tankFire : MonoBehaviour
    6. {
    7.     public float soundVolume;
    8.     public AudioClip fireSound;
    9.     public AudioSource mySource;
    10.  
    11.     public float projectileSpeed;
    12.     public float projectileSize;
    13.  
    14.     public GameObject fireSmoke;
    15.     public GameObject projectile;
    16.     public GameObject fireFire;
    17.  
    18.     public Transform turretPivot;
    19.     public Transform gunPivot;
    20.     public Transform projectileLoc;
    21.  
    22.     public float baseTimer;
    23.     public float timer;
    24.     public float turretRotationRange;
    25.     public Transform enemy;
    26.     Quaternion targetRotation;
    27.     Vector3 targetDirection;
    28.     void Start()
    29.     {
    30.         mySettings.effectVolume = 1f;
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (Input.GetKeyDown(KeyCode.K)) fire();
    37.         rotateTurret();
    38.     }
    39.  
    40.  
    41.     void rotateTurret()
    42.     {
    43.         targetDirection = enemy.position - gunPivot.position;
    44.         targetRotation = Quaternion.LookRotation(targetDirection);
    45.         Quaternion gunRotation = targetRotation;
    46.         gunRotation = Quaternion.Euler(0f, gunRotation.eulerAngles.y, 0f);
    47.         float angle = Quaternion.Angle(transform.rotation, targetRotation);
    48.         timer += Time.deltaTime;
    49.         //if (angle > turretRotationRange) return;
    50.  
    51.         gunPivot.rotation = Quaternion.Lerp(gunPivot.rotation, gunRotation, 5f * Time.deltaTime);
    52.     }
    53.  
    54.     void calculateTimer()
    55.     {
    56.         timer += Time.deltaTime;
    57.         if (timer >= baseTimer)
    58.         {
    59.             fire();
    60.             timer = 0f;
    61.         }
    62.     }
    63.     void fire()
    64.     {
    65.         mySource.PlayOneShot(fireSound, soundVolume * mySettings.effectVolume);
    66.         GameObject currentBullet = Instantiate(projectile, projectileLoc.position, targetRotation);
    67.         currentBullet.transform.localScale *= projectileSize;
    68.         currentBullet.GetComponent<Rigidbody>().velocity = targetDirection.normalized * projectileSpeed;
    69.         Instantiate(fireSmoke, projectileLoc.position, targetRotation);
    70.     }
    71. }
    72.  
     
  2. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    401
    my bad its working there was 2 same script thats why it wasnt working