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 cyangamer · Jun 11, 2010 at 01:44 AM · physicsraycastcharactercontrollerboxcollider

Need help with Raycasting

I don't know what I'm doing wrong, so hopefully someone will want to give some quick advice.

The player object is controlled by the keyboard (WASD, Enter/Return), and it activates objects around him when the player presses Enter. I don't want the object to activate unless the player is facing the object, so I tried to use Raycasting from the player to the collider object (now simply a cube) that surrounds the object:

//Get the ray coming from Player
public bool IsFacingObject()
{
    return (Physics.Raycast(transform.position, transform.forward, 10));
}

The above method returns true if the ray coming from the player hits something, false if it doesn't.

Now for the Box collider:

void Update()
{
    if (canTalk && Input.GetButtonDown("Enter") && player.GetComponent<LukaCController>().IsFacingObject())
    {
        if (GetComponent<MessageBoxScript>())
        {
            Destroy(GetComponent<MessageBoxScript>());
        }else
        {
            begin = gameObject.AddComponent<MessageBoxScript>();
            begin.MessageBox(font1, font2, faceGraphic,
                0, "Convo Box", true, "Hi, I'm the Convo Box!$ How are you?");
        }
    }

}

canTalk is a boolean that returns true if the player's collider hits the trigger that surrounds the cube. player.GetComponent().IsFacingObject() is the method I showed you above, returning true or false.

However, player.GetComponent().IsFacingObject() doesn't return true, even though the player is facing the cube and is even touching the cube. Why isn't the raycast seeing the cube?

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
2
Best Answer

Answer by Ricardo · Jun 11, 2010 at 07:06 AM

Start by using Debug.DrawRay on IsFacingObject to ensure that the raycast would actually be hitting something. Without seeing the scene, it's possible that your object's pivot (transform.position) is higher than the top of the cube, and that your ray is overshooting it.

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 cyangamer · Jun 12, 2010 at 05:03 AM 0
Share

That was basically it. The ray was undershooting the cube and missing the mark. It is fixed now. Thanks!

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

No one has followed this question yet.

Related Questions

Can a character controller collide with a trigger? 2 Answers

Can't raycast on Character Controller? 1 Answer

How can I make the Player [CharController] interact with Physic Materials 0 Answers

character controller + physics 1 Answer

Hovercraft Physics Problems 1 Answer


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