Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DMCH · Feb 11, 2013 at 10:36 PM · rotationraycast

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 12, 2013 at 03:03 AM 0
Share

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?

avatar image DMCH · Feb 12, 2013 at 01:09 PM 0
Share

Yes, a prefab. When the enemy is dragged on, its rotation is 0,0,0. It is facing down the map. Any ideas?

avatar image robertbu · Feb 12, 2013 at 02:37 PM 0
Share

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)?

avatar image DMCH · Feb 12, 2013 at 02:54 PM 0
Share

I've dragged an enemy onto the scene, and taken some screenshots of the scene and the inspector. Thanks for helping!

alt text

raycast inspector.png (23.9 kB)
raycast.png (118.4 kB)
avatar image robertbu · Feb 12, 2013 at 03:46 PM 1
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DMCH · Feb 15, 2013 at 02:02 PM 0
Share

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);

avatar image
0

Answer by Imankit · Feb 12, 2013 at 05:42 AM

Use transform.TransformDirection(Vector3.Forward) instead of transform.forward

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DMCH · Feb 12, 2013 at 01:05 PM 0
Share

Thanks for the suggestion. The ray is still being cast out to the back of the characters. Any other ideas?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges