Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by justahobbyisthere · Apr 25, 2018 at 12:41 AM · raycastraycasthit

Raycasts stop working after ~3 seconds

So in the project i'm working on you're suppose to be able to look at an object and pick it up, I've got that pretty much figured out but when I play the game i can pick up any object I like but after a little bit of time the object will fall out of my "hands". As per this I thought it had to do with rigidbodies so I removed them to no avail. As as per this I thought it had to do with Capsule Colliders, but a lass Unity's standard FPS controller does not use them.
The symptoms of my problem can be seen here. In the video the both boxes have a ridgidbody component but the second is is kinamatic. It seems like the raycast does not register that it should be hitting anything for an instant, letting the box go, but im not sure. Any ideas?

Also heres the relevant code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class CrossHair : MonoBehaviour {
     #region variables
     [Header("Colors of crosshair")]
     [Tooltip("Colour when you're not touching something, RGBA")]
     public Vector4 default_colour;
     [Tooltip("Colour when you're touching something, RGBA")]
     public Vector4 highlight_colour;
 
     private Image cross_hair_img_component;
 
     //stuff for the raycast
     public Camera raycast_camera;
     public float crosshair_range;
     RaycastHit crosshair_hit;
 
     //stuff relating to the object being picked up
     [Header("Crosshair hit's object's properties")]
     [Tooltip("distance between the object and the camera when picking the object up")]
     [SerializeField] private float object_distance;  //object distance
     private bool is_Interactive;    //true if object is interactive
     #endregion
 
     // Use this for initialization
     void Start () {
         #region gets cross_hair_img_component
         //gets the gets the child (cross_hair_img) of canvas_for_cross_hair in the hiarchary
         //which is a child of the gameobject (FirstPersonCharacter) then gets the Image component of that child
         Transform canvas_in_hierarchy = gameObject.transform.Find("canvas_for_cross_hair");
         Transform cross_hair_in_hierarchy = canvas_in_hierarchy.transform.Find("cross_hair_img");
         cross_hair_img_component = cross_hair_in_hierarchy.GetComponent<Image> ();
         #endregion
     }
     
     // Update is called once per frame
     void Update () {
         raycast_crosshair();
         if (is_Interactive == true) {
             if (Input.GetKey ("e") == true) {
                 pick_up_object ();
             }
             change_crosshair_colour ();
         } else {
             change_crosshair_colour_to_default ();
         }
     }
 
     void raycast_crosshair () {
         if (Physics.Raycast (raycast_camera.transform.position, raycast_camera.transform.forward, out crosshair_hit, crosshair_range)) { //shoots a raycast
             if (crosshair_hit.transform.tag == "Interactive") { //if hit, checks if object is interactive
                 is_Interactive = true;
             } 
         } else {
             is_Interactive = false;
         }
     }
 
     void change_crosshair_colour () {
             cross_hair_img_component.color = highlight_colour;
     }
     void change_crosshair_colour_to_default () {
         cross_hair_img_component.color = default_colour;
 
     }
 
     void pick_up_object () {
         crosshair_hit.transform.position = raycast_camera.transform.position + raycast_camera.transform.forward * object_distance; //object in front of camera
         crosshair_hit.transform.rotation = new Quaternion( 0.0f, raycast_camera.transform.rotation.y, 0.0f, raycast_camera.transform.rotation.w ); //object rotates with camera
     }
 }
 

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 vinilly · Apr 25, 2018 at 08:36 AM

Use this to see where the ray is going on Scene..

Debug.DrawRay(raycast_camera.transform.position, raycast_camera.transform.forward, Color.red);

Comment
Add comment · Show 2 · 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 vinilly · Apr 25, 2018 at 09:09 AM 0
Share

trying to see what is happening first..

avatar image justahobbyisthere vinilly · Apr 25, 2018 at 02:08 PM 0
Share

hey thanks, that was useful, it confirmed that the raycast does seem to be intersecting with the object the whole time.

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

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

Related Questions

RayCast2D and RayDraw errors 0 Answers

Swipe single object at a time upon touch, not swiping allover the screen 0 Answers

BulletHole and HitSparks spawning in front of weapon 0 Answers

How to freeze an GameObject on x axis in specific direction? 1 Answer

Player in front of enemy check not working, Physics.Raycast()? 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