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 Ves · Mar 03, 2013 at 10:13 PM · movementraycastcolliderfixedupdate

Raycasting on Moving Colliders

I perform raycasting on units that are moving in my 2D RTS-style game in order to select it and return information about it. However, I cannot get the raycasting to perform correctly while the unit is moving.

The movement takes place in the FixedUpdate function on the individual unit itself (i.e. there is a Movement script attached to the unit that performs the moving). The Raycast for selecting is done in a Manager script, and is also in a FixedUpdate function. However, no matter what type of update function I put the Raycast in, it does not detect the unit while it's moving.

The units only contain box colliders (no rigidbodies), and are moved using transform.Translate().

How can I get raycast to work on colliders that are moving?

Relevant code:

Movement function:

     def FixedUpdate():
         if not path:
             return
         
         if currentWaypoint >= path.vectorPath.Count:
             Debug.Log("End of path reached.")
             return
             
         dir as Vector3 = (path.vectorPath[currentWaypoint] - transform.position).normalized
         dir *= speed * Time.fixedDeltaTime
         SimpleMove(dir)
         
         if Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance:
             currentWaypoint++
             return
             
     def SimpleMove(dir as Vector3):
         #rigidbody.MovePosition(rigidbody.position + dir)
         transform.Translate(dir)

Mouse event function:

     def Update():
         if Input.GetMouseButtonDown(0):
             ray as Ray = camera.main.ScreenPointToRay(Input.mousePosition)
             hit as RaycastHit
             if Physics.Raycast(ray, hit, 100):
                 if not hit.transform.CompareTag("Background"):
                     Debug.Log("We're selecting: " + hit.transform.name)
                     selectedUnit = hit.transform.GetComponent[of Unit]()
                 else:
                     selectedUnit = null
Comment
Add comment · Show 4
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 Chimera3D · Mar 03, 2013 at 10:34 PM 0
Share

Try just using a normal update function for the manager script (the one the raycast is in).

avatar image Ves · Mar 03, 2013 at 10:46 PM 0
Share

Thanks for the suggestion. Unfortunately, I've already tried that. It doesn't seem to matter what update function I put the Raycast in; whether Update(), FixedUpdate(), or LateUpdate(), I still can't detect the collider while it's moving.

avatar image Bunny83 · Mar 03, 2013 at 10:55 PM 0
Share

When you don't use physics or have a higher order movement calculation (something that involves t² or t³) there's no reason why you should put it in FixedUpdate. Never use FixedUpdate to process one-time input events like key down / mouse button down. FixedUpdate don't run every frame if you have a high frame rate so it might miss the frames where the down / up event happened.

It would help to see the relevent parts of your script.

avatar image Ves · Mar 03, 2013 at 11:15 PM 0
Share

The reason the movement calculation is in the FixedUpdate function is because colliders' positions are only updated every FixedUpdate. It was an attempt to make sure the mouse events were occurring at a time when the position of the collider was where it really was. Question edited with relevant code.

0 Replies

· Add your reply
  • Sort: 

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

12 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

Related Questions

Move player to specific location and avoid obstacle. 2 Answers

Moving objects using raycasting? 1 Answer

Making object found with raycast a child of the object the ray is cast from 1 Answer

Enemy AI passing through walls 1 Answer

Raycasts left/right problem. One works, other doesn't...? 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