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 RedOneDigitals · Mar 27, 2014 at 11:07 PM · rigidbodyraycastmovingcapsule

Physics.Raycast not working as expected!

Hi! :) I am trying to make my moving capsule to stop when it detects the object tagged "Finish" coming in front of it by using a raycast to detected and check for coming object within distance specified in "sensDistance", but the capsule keep going like the raycast is not working or something, i really couldn't figure out the problem and i dont know if i am missing something about raycasts. this is my piece of code and please help ASAP:

 public float speed = 110f;
 bool front;
 float sensDistance = 3f;
 RaycastHit frontHit;
 Ray forwardRay;
 float sensDistance = 3f;
 
 
 void Start() {
 forwardRay = new Ray(transform.position,transform.TransformDirection(Vector3.forward));
 }
 
 void Update() {
 // 
      Debug.DrawRay(transform.position, forwardRay.direction * sensDistance, Color.red);
 
      front = Physics.Raycast (forwardRay, out frontHit, sensDistance);
 //
      if (!front)
          Move (Vector3.forward, speed);
      else {
      if(frontHit.collider.tag == "Finish" )
          StopMove();
      }
 }
 
 void Move(Vector3 direction, float _speed) {
      rigidbody.velocity = transform.TransformDirection ((direction * _speed *  Time.deltaTime));
 }
 
 void StopMove() {
      rigidbody.velocity = Vector3.zero;
 }
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
Best Answer

Answer by robertbu · Mar 28, 2014 at 03:34 AM

One potentially serious issue with your code is that you set the position and the direction for the Raycast() in Start(). That means it never changes even when the object moves or changes direction. There is a version of Raycast that takes a position and a direction, so you don't need to construct a ray. So get rid of the ray, and do this for your Raycast():

  front = Physics.Raycast(transform.position, transform.forward, out frontHit, sensDistance); 

And your Debug.DrawRay() then becomes:

  Debug.DrawRay(transform.position, transform.forward * sensDistance, Color.red);
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 RedOneDigitals · Mar 28, 2014 at 11:48 AM 0
Share

Thank you! it works fine..

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

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

Related Questions

Attaching 2 objects using joints and raycasting 0 Answers

raycast rigidbody addforce 2 Answers

How to get right Physics with Follow on click 1 Answer

How to stop waypoints script in another script? 0 Answers

How to access component that the raycast hit 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