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 /
avatar image
1
Question by RyanAchtenSoma · May 11, 2016 at 09:33 AM · raycastpositionpoint

Moving object with raycast issues

Hi Unity Community,

I have written a script where a gameobject is intended to move to a raycast.point thrown from the player camera. For the most part this works fine, however there are times (approximately when camera is 45 degrees up from the object) when the object rapidly moves towards the camera (i.e. raycast source).

I have tried a number of approaches attempting to resolve this, however I can’t seem to dig out the root of this issue. If anyone can provide any pointers as to where I am going wrong I would be incredibly grateful.

NB: coding in uJS

Many thanks in advance, Ryan

 function FixedUpdate()
 {
 
     if (modObj != null && !guiMode){
     
         //Panel Control 
         if (!selectObjPanel.activeSelf && !modifySelectObjPanel.activeSelf) //if the selectpanel not open and modSelect not already activated
         {
             activateModSelectObjPanel(true); //activate it
         } 
         else if (selectObjPanel.activeSelf)
         {
             activateModSelectObjPanel(false);
         }
 
     
         //Move
         if ( Input.GetKey(KeyCode.E))
         {    
             if (Input.GetKeyDown(KeyCode.E))
             {
 //                modObj.GetComponent(BoxCollider).enabled = false;
                 modObj.GetComponent(Rigidbody).isKinematic = true;
                 modObj.GetComponent(Rigidbody).useGravity = false;
 //                
                 initPos = modObj.transform.position;
                 var initRotation = modObj.transform.rotation;    
             }
     
             moveObject(modObj, initPos, initRotation);
         }
         else{
 //            modObj.GetComponent(BoxCollider).enabled = true;
             modObj.GetComponent(Rigidbody).isKinematic = false;
             modObj.GetComponent(Rigidbody).useGravity = true;
         }
     }
 }
 
 function moveObject(modObj : GameObject, initPos : Vector3, initRotation : Quaternion)
 {
     //Debug.Log("Moving Object");
 
     var hit : RaycastHit;
     var foundHit : boolean = false;
     
     foundHit = Physics.Raycast(transform.position, transform.forward, hit);
     //Debug.DrawRay(transform.position, transform.forward, Color.blue);
     
     if(foundHit && hit.transform.tag != "Player")
     {
         //Debug.Log("Move to Hit Point: " + hit.point);
         modifyObjGUIscript.activateMoveDisplay(initPos, hit.point);        
         
         var meshHalfHeight = modObj.GetComponent.<MeshRenderer>().bounds.size.y /2; //helps account for large and small objects
 //        Debug.Log("CurObj Mesh Min: " + meshHalfHeight);
         
 //        modObj.transform.position = hit.point; //***method 01***
 //        modObj.transform.position = Vector3.Lerp(initPos, hit.point, speed); //***method 02***
 //        modObj.transform.position = Vector3.SmoothDamp(initPos, hit.point, velocity, smoothTime); //***method 02***
 
         var rb = modObj.GetComponent.<Rigidbody>(); 
         rb.MovePosition(hit.point); //***method 03***
         
         modObj.transform.position.y =  modObj.transform.position.y + meshHalfHeight + hoverHeight;
         
         modObj.transform.rotation = initRotation;
     }
 }
 
Comment
Add comment · Show 1
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 RyanAchtenSoma · May 12, 2016 at 02:26 AM 0
Share

$$anonymous$$anaged to prevent this from occurring by deactivating the collider attached to the object being moved. However I need the collider for various reasons so this approach is not appropriate. Any alternative suggestions?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by RyanAchtenSoma · May 12, 2016 at 03:04 AM

Turns out the issue was being caused by the raycast hitting the object being moved. Resolved this by only allowing hits from the terrain to be used as points to move to.

 if(foundHit && hit.transform.tag == "Terrain")
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
1

Answer by aimmmmmmmmm · May 12, 2016 at 06:21 AM

The raycast hitting the object you are moving, causing the raycast's point to move towards the camera. Then the object moves, causing the point to move towards the camera again. Your best bet to solving this issue is to assign a layer to the object you are creating, and then create a layermask and use it during your raycasting to ignore the object you are moving.

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

58 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

Related Questions

Move player with mouse help 0 Answers

interesting raycast question 2 Answers

Collision point on surface? 1 Answer

RayCast relative to camera stays in 1 place. 2 Answers

Find a point at a distance from point on a line passing through two points 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