Search Unity

transform.LookAt is not rotating my model correctly, but my raycasting from it works fine.

Discussion in 'Scripting' started by cinderousrex, Sep 20, 2019.

  1. cinderousrex

    cinderousrex

    Joined:
    May 21, 2019
    Posts:
    43
    I have a few turrets which i want to aim at the aim at the mouse.

    so far my best solution is to use
    Code (CSharp):
    1. barrelToRotate.transform.LookAt(hit.point);
    but in game no matter how I use this the model doesn't actually face towards the object (IT FACES towards another direction)

    Ive tried this with simple code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotateTest : MonoBehaviour
    6. {
    7.  
    8.     public GameObject barrelToRotate;
    9.     public GameObject toBeRotatedTo;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.      
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (Input.GetButtonDown("Jump")) {
    21.  
    22.             barrelToRotate.transform.LookAt(toBeRotatedTo.transform.position);
    23.         }
    24.     }
    25. }
    26.  
    Is there something wrong with my prefab? Do I have to rotate the model a certain way before this? Im not entirely sure why it just doesn't rotate correctly. It must have something to do with the model itself, but im not sure...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    The .LookAt() means "align this transform so that the +Z axis goes from where I am to what I am looking at."

    If your prefab does not have the gun barrel pointing +Z, then yes, you need to adjust your prefab. This should often be trivial, either by spinning a child object, or adding an extra GameObject and pivoting off that.
     
  3. cinderousrex

    cinderousrex

    Joined:
    May 21, 2019
    Posts:
    43
    Well whatever it is I cannot figure it out.

    Ive tried rotating every little piece of my prefab to different locations and I cannot seem to fix it.

    I have a debug draw ray that uses its direction as transform.forward (which from my understanding takes the gameobject and sets the direction outwards in front of the game object.)

    Code (CSharp):
    1.             subTurretsArray = GameObject.FindGameObjectsWithTag("SubTurret");
    2.             foreach (GameObject subturret in subTurretsArray)
    3.             {
    4.                 GameObject subturrettemp = subturret.gameObject.transform.GetChild(0).GetChild(1).GetChild(0).gameObject;
    5.  
    6.                 Debug.DrawRay(subturrettemp.transform.position, subturrettemp.transform.forward * 2000, Color.red);
    7.  
    8.             }
    Yet when I tell my turret barrel to rotate towards the hit point ive collected from a raycast it rotates but the barrel is not rotated to be in the front. Im not sure what the hell is going on because ive tried setting all rotations to 0 in prefabs,ect. then running. I know its pointing forward because i also shoot a ray out from the front of the rotated game object which can destroy targets it hits. so its using the same logic to shoot forward as it is to draw the ray, what causes the model to be off rotation by like 90 degrees id say?

    Code (CSharp):
    1.                     barrelToRotate = subturret.gameObject.transform.GetChild(0).GetChild(1).GetChild(0).gameObject;
    2.                    
    3.                     barrelToRotate.transform.LookAt(hit.point);
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Run your game until you can see the problem happen.

    Press Pause in the editor.

    In the upper left, select "Translate" (the arrow-tipped plus sign, shortcut is W)

    In the upper left, make sure context boxes says Pivot and Global

    Slowly start selecting the things in your scene that are pointing the wrong way.

    The blue arrow you see is +Z direction. The red is +X and the green is +Y.

    The +Z is the "LookAt direction."

    When you find out what part of your prefab/scene/model is not set up properly, fix it.
     
  5. cinderousrex

    cinderousrex

    Joined:
    May 21, 2019
    Posts:
    43
    I can see the problem you describe. When looking at these turrets in their raw prefab they don't face towards the front as well as when I stop the scene and look at the axises (it doesn't point towards the blue). I can set my view to front and it will show it on its side. Despite this, I can manually change the rotation to fix the starter problem. Then when it goes into the scene, it keeps its rotation which is great, but still when I use transform.LookAt I run into the issue where it points from its side, not the front. ooof.

    Is it something easy im missing? Like how do you change the actual prefab to face the right way?


     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Easy! Insert an extra GameObject, usually at the root, and then rotate everything below it to face the correct direction. Refer to the final words in my original post. :)