- Home /
transform.TransformDirection not working
I'm working on a Line of sight script. It works completely fine exept when my enemy rotates the forward position doesnt get updated. My Line of sight script is made so that it doesn't detect the player if he's behind the enemy. So I write: Debug.Log (Model.TransformDirection(RayHelp.eulerAngles)); When rotating my enemy nothing happens! (RayHelp is used to look at the player, Model is useless, it was just me trying uot if it would fix it. When removing it it still doesnt work) If you want to see the full code here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LOS : MonoBehaviour {
public Transform Goal;
public Transform RayHelp;
public Transform RayHelpParent;
public Transform Model;
public float maxDistance = 100;
public float lodAngle = 90;
void Start () {
}
void Update () {
RayHelp.position = RayHelpParent.position;
RaycastHit hit;
RayHelp.LookAt(new Vector3(Goal.position.x,RayHelp.position.y,Goal.position.z));
RayHelp.eulerAngles = new Vector3 (RayHelp.eulerAngles.x,RayHelp.eulerAngles.y + 90,RayHelp.eulerAngles.z);
if(transform.TransformDirection(RayHelp.eulerAngles).y > 180){
RayHelp.eulerAngles = new Vector3 (RayHelp.eulerAngles.x,RayHelp.eulerAngles.y - 180,RayHelp.eulerAngles.z);
RayHelp.eulerAngles = new Vector3 (RayHelp.eulerAngles.x,Vector3.Distance(RayHelp.eulerAngles, new Vector3(RayHelp.eulerAngles.x,180,RayHelp.eulerAngles.z)),RayHelp.eulerAngles.z);
}
if(Physics.Raycast(RayHelp.position,-(RayHelp.position - Goal.position), out hit,maxDistance)){
Debug.Log (Model.TransformDirection(RayHelp.eulerAngles));
if(transform.TransformDirection(RayHelp.eulerAngles).y < lodAngle){
if(hit.transform.tag == "Player"){
Debug.Log("!");
}
}
}
}
}
Answer by Bunny83 · Oct 24, 2017 at 09:52 PM
EulerAngles is not a direction vector but simply a collection of 3 angles. They can't be "transformed" into another space as they actually rely on the axes you provide them.
So you can be sure that TransformDirection works when you actually pass a direction vector defined in local space.
Thanks! (this is actuarly the first time I asked a question here so this is really cool). I ended up using RayHelp.right. Some code was useless so I deleted it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612141739im_/https://answers.unity.com/themes/thub/images/avi.jpg)