- Home /
Raycast to transform.forward comes out rear of Gameobject
UPDATE BELOW **
Hello,
I'm trying to build a basic obstacle avoidance ai for a top-down shooter using ray cast. To cut a long story short, I found the rays were being cast out the enemies' backs, not straight, but at an angle. Any help on figuring out why the ray is not being cast in the right direction would be hugely appreciated!
Here's part of the code for the obstacle avoidance
// Get targetHeading/Direction to Target
targetHeading = targetTurretPosition - transform.position;
targetDirection = targetHeading.normalized;
// Declare RayCashHit
RaycastHit hit;
// Raycast forward from transform, to a distance of 2
if(Physics.Raycast(transform.position, transform.forward, out hit, 4f))
{
// Make sure ray is not hitting the gameobject
if(hit.transform != transform)
{
Debug.Log("EnemyScript: RayHit");
Debug.DrawLine(transform.position, hit.point, Color.red);
// Add direction from hit face, times how much force to repel by
targetDirection += hit.normal * 100;
rayhit = true;
}
}
Here's how the enemies are being created:
GameObject go = (GameObject) Instantiate(enemy, activeSpawn, Quaternion.identity);
Please let me know if you need any more info to solve this problem!
UPDATE 1 **
I've tried adding a number of empty child gameobjects around the enemy, roughly at 45 degree intervals. These are declared as public gameobjects in the script, and assigned in the inspector. There is no appreciable difference in where the ray is being cast to, regardless of which sensor the ray is directed to go.
What is enemy (prefab I assume)? If you dragged one in the scene and set the rotation to (0,0,0), what direction would it face?
Yes, a prefab. When the enemy is dragged on, its rotation is 0,0,0. It is facing down the map. Any ideas?
By down the map do you mean the enemy is facing positive Z with its head up when it has a rotation of (0,0,0)?
I've dragged an enemy onto the scene, and taken some screenshots of the scene and the inspector. Thanks for helping!
If you've drawn your arrows correctly, your enemy forward is facing negative Z rather than positive Z. You can parent your enemy to an empty game object and rotate your enemy to face positive Z. Your script would need to be attached to the empty game object.
Answer by RyanZimmerman87 · Feb 13, 2013 at 10:25 PM
Is it possible your enemy object is just facing the wrong direction? Maybe try using:
transform.LookAt(theVectorPosition or gameObject.transform.position you want);
maybe if you do that right before you cast your ray it might work?
That's more or less what the problem was. Rotating the spritesheet and removing the y+180 from the below line of code sorted it out. Thanks!
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y+180, 0);
Answer by Imankit · Feb 12, 2013 at 05:42 AM
Use transform.TransformDirection(Vector3.Forward) instead of transform.forward
Thanks for the suggestion. The ray is still being cast out to the back of the characters. Any other ideas?
Your answer
Follow this Question
Related Questions
click to move workig script!! but pls help with rotation!! :( 1 Answer
Make 2 object not go into each other when modifying one object's position 0 Answers
Unity Rotate Raycast on Quaternion 1 Answer
Cant understand why object rotates when setting transform.up to normal 2 Answers
Automatically rotating objects to fit on to others 0 Answers