Raycast moves away from object when object is rotated.

i have a game object with a script attached. I am casting a series of rays around the object. The problem is that when i move the object away from world origin and rotate it this causes the ray direction to move in the opposite direction. The problem doesn’t occur if I move the object or rotate the object. It only happens when i do both. Here’s the code. Thanks in advance.

	for (int i = 0; i < NumOfRays; i++) {
		x = Mathf.Sin (angle);
		z = Mathf.Cos (angle);
		angle += 2 * Mathf.PI / NumOfRays;

		Vector3 dir = transform.TransformDirection( new Vector3 (transform.position.x + x, 0, transform.position.z + z));
		Debug.DrawLine (transform.position, dir, Color.red);

		if (Input.GetKeyDown(KeyCode.UpArrow))
		rb.AddForce (Vector3.forward);
	}
	angle = 0;
}

The line should be

Vector3 dir = transform.TransformDirection(new Vector3(x, 0, z));

because TransformDirection takes in a local space direction and outputs a world space direction (where the vector has been rotated by transform.rotation).

Also use Debug.DrawRay instead of DrawLine.

Thank you,
Stupid me didn’t read your response properly. so i spent he last hour remaining confused.
Thanks again.