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 Hamesh81 · Feb 10, 2015 at 11:15 AM · rotationraycastanglenormalface

Why does the raycast hit only objects/faces of a certain rotation?

I am getting some very strange behaviour from a simple raycast script that I created. I have a simple FPC with the below script attached:

 using UnityEngine;
 using System.Collections;
     
 public class RaycastTest : MonoBehaviour {
 
     private Transform characterTransform;
     private Rigidbody characterRigidbody;
     public MouseLook FPCMouseLook;
     public Controller characterController;
     public Transform camera;
     public SmoothFollow cameraSmoothFollow;
     public float coverAdjustSpeed = 1;
     public float coverOffset = 0.5f;
     public float raycastLength = 2;
     public float raycastYOffset = 0.5f;
 
     // Use this for initialization
     void Start () {
 
         characterTransform = transform;
         characterRigidbody = GetComponent<Rigidbody>();;
         FPCMouseLook = GetComponentInChildren<MouseLook>();
         characterController = GetComponentInChildren<Controller>();
         cameraSmoothFollow = camera.GetComponent("SmoothFollow") as SmoothFollow;
     }
     
     // Update is called once per frame
     void Update () {
 
         RaycastHit hit;
         Vector3 raycastPosition = new Vector3(characterTransform.position.x, characterTransform.position.y + raycastYOffset, characterTransform.position.z);
 
         if (Physics.Raycast(raycastPosition, Vector3.forward, out hit, raycastLength)) {
             Vector3 rayHitPosition = hit.point;
             float distanceFromWall = Vector3.Distance(characterTransform.position, hit.point);
 
             Vector3 debugForward = transform.TransformDirection(Vector3.forward) * raycastLength;
             Debug.DrawRay(raycastPosition, debugForward, Color.yellow);
 
             Debug.Log ("Distance from Wall is: " + distanceFromWall);
         }
     }
 }

For some reason when I get within range of the left box and bottom box the raycast hits, but it does not hit for the right box or the top box not matter how close I get. Even stranger, if I start to rotate the right box while the player's raycast is with range, after rotating the box to a certain angle the raycast suddenly detects the box.

alt text alt text

I have been trying to work out why this is happening for the last two days but I cannot understand it. All 4 boxes are exact duplicates with the blue sections being triggers and the red sections containing box colliders. Both blue and red sections are children of an empty gameobject and the FPC is a simple rigidbody with capsule collider setup. Can someone please explain to me why this is happening?

raycast1.png (137.4 kB)
raycast2.png (91.1 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lordlycastle · Feb 22, 2015 at 10:44 AM

You set the direction vector for the ray-cast to Vector3.Forward, which is not always the case. It works alright for left and bottom, because they are both at an angle. If they were L shaped it would only work for left box. Anyways, instead of using Vector3.forward which is just shorthand for [0,0,1], you should be using characterTransform.forward which is the vector for the z-axis of the transform in world space.

Hope this solves your problem.

Comment
Add comment · 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

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.Slerp normal 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

default cylinder to indicate hit.normal 1 Answer

How to detect object by rotations-raycast? 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