0
我正在嘗試在Unity 2d中創建視覺字段。我從一個旋轉的遊戲物體投射光線投射,它根據角色的位置旋轉。然而,這個字符來回移動,所以當他們向左移動時,我將transform.scale設置爲-1。問題是,當我這樣做時,raycast不會隨着角色改變方向並保持指向正確!這是代碼,我可能錯過了一些明顯的東西!任何幫助,將不勝感激!從旋轉變換Unity 2D Raycasting
Transform parent;
float vision_angle_ = 50;
float direction;
Vector3 angle;
Vector2 position;
Quaternion q;
int x = 10;
void Start()
{
parent = transform.parent;
}
void Update()
{
direction = parent.GetComponent<Behaviour>().direction;
angle = new Vector2 (x, Mathf.Tan ((vision_angle_) * .5f * Mathf.Deg2Rad) * x);
q = Quaternion.AngleAxis (1, -transform.forward);
position = new Vector2 (transform.position.x, transform.position.y);
for (float i = 0; i < 50; i++) {
angle = q * angle;
RaycastHit2D tile_hit = Physics2D.Raycast (position, transform.TransformDirection (angle), 10);
Debug.DrawRay (position, transform.TransformDirection (angle), Color.green);
}
}