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
1
Question by Andrew 12 · Feb 20, 2011 at 08:11 PM · cameraraycastgravity

Drifting Raycast in Camera?

I'm making basic item pickup like you would see in most first person games (Oblivion, Bioshock). Point the center of the camera to the item, click, and you have the item in your inventory. So I have a script attached to a camera (parented to an out of the box character controller) that sends out a raycast and then do some logic on the game object it hits.

//...
hit = new Raycast()
//...
void Update(){
if (Physics.Raycast(transform.position, transform.forward, out hit, 75f)){
    //we hit something
    lastOrigin = transform.position;
    if(hit.transform != null){          
    lastPoint = hit.transform.position;
   }
}
//if we want to interact with an object
if (Input.GetMouseButtonDown(0) || Input.GetButtonDown("Fire1"))  //Left Click
{
  //check if we're looking at any object
  if ((hit.collider != null) && (hit.collider.gameObject != null) )
      {
          //OMITTED, ASSUMED UNIMPORTANT
      }
 }
//Draw where the last valid raycast was.
Debug.DrawLine(transform.position, lastPoint, Color.red);
}

Now, this works. To a point. I noticed after awhile my collisions were sort of flickering. So I ran a debug draw to see the raycast.

And got this. http://imgur.com/a/ukOaZ

If my screencaps aren't enough to explain it I noticed the origin point of the raycast was slowly sinking into the ground, but popping back to the place I wanted it to be. That means the transform position of the camera is drifting?! What?! Is raycast affected by Gravity by default? Is my Camera? Am I drawing the right thing? Has anyone else found this? Can anyone explain it?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Andrew 12 · Feb 24, 2011 at 12:35 PM

Figured it out. Looks like cameras hate being parented to gameobjects with rigidbodies. They must be inheriting some unseen property that's causing them to be affected by gravity, or at least the physics components that I'm declaring inside them are affected at any rate.

Removed the rigidbody from my parent object and the drifting stopped.

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

Answer by fenderrex · Dec 13, 2017 at 09:00 PM

I would like to bump this. I have a ray cast checking the normal of the ground and it works until the ray cast in below the terrain! this makes if (Physics.Raycast(ray, out hit)) return false intermittently until its far below. if I jump it starts working again. Iv changed ray.origin and the Ray instantiation with transform.position even debugged "printing" the transform.position is as expected... the ray.origin prints as expected pos but Debug.DrawLine(transform.position, hit.point, Color.red); drifts down! sometimes the direction will flip to look at the hit point without if (Physics.Raycast(ray, out hit)) is removed... ill look into the rigid body effects soon. anyone know of any documentation?

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

1 Person is following this question.

avatar image

Related Questions

Raycast misses after camera zoom 2 Answers

Problem with gravity in a Third Person Controller 0 Answers

How to 'gravitize' an object/camera to another sphere... 1 Answer

Only show GameObject if at least 1 pixel is rendered 0 Answers

Follow mouse cursor (with Y = 0)? 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