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 T27M · Sep 22, 2012 at 07:41 PM · c#colliderinterface

What is causing this to happen?

 public class Selection_Control : MonoBehaviour {
     
     // Variable to hold a selected gameobject    
     public GameObject activeObject;
 
     
     
     // Update is called once per frame
     void Update () {
         
         // Delcare a ray from the camera to the mouse input
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         
         // Get the mouse position
         Vector3 mousePos = Input.mousePosition;
         
         // Right click to select a game object.
         if (Input.GetMouseButtonUp ( 1 ) )
         {
             // Ignores gameobjects that are tagged as "Static"                        
             if(Physics.Raycast(ray, out hit, 100) && !hit.collider.gameObject.CompareTag("Static"))
             {
                 // Draw the ray
                 Debug.DrawLine(ray.origin, hit.point, Color.green);
                 
                 // Assign the hit.collide.gameobject as out acitve object
                 activeObject = hit.collider.gameObject;
                             
                 Debug.Log( "Selected " + activeObject );
             }
             else
             {
                 Debug.Log("This is a static object");
             }
                 
         }
         
         //Check to see if the active object still under the mouse
         if(Physics.Raycast(ray, out hit, 100) && hit.collider.gameObject == activeObject)
             {
             
                 // Draw the ray
                 Debug.DrawLine(ray.origin, hit.point, Color.green);
                 
                 // Check to see if the left mouse button is held down.
                 if ( Input.GetMouseButton ( 0 ) )
                 {
                     // Update the active objects position with that of the mouse's position
                     activeObject.transform.position = Camera.main.ScreenToWorldPoint( new Vector3( mousePos.x, mousePos.y, 5.6f ) );
                     
                     
                 }
                             
                 
             }
         
         
     
     
     
         
     }
     
     
     
         
         
     
     
 }

I was messing around and made this script to allow selection and movement of objects via the mouse. It works fine with objects that don't have a rigid-body component but those that do have one behave strangely.

When I move the object around with a rigid-body after releasing the object it will sometimes fall through the terrain for no reason. I have checked and both the terrain and the object have colliders on them.

If it touches another object it will shoot off in a random direction and rotation, sometimes it will do this even when it doesn't hit anything else.

What am I doing wrong here ?

Thanks

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 alpaca of zion · Sep 22, 2012 at 08:06 PM

Gravity happens on the y axis. You aren't constraining that axis so objects can move below the terrain.

When you move a game object by specifying an absolute position like this, the physics engine has to move things out of the way to make space for your object. If they are overlapping much this can create a very strong push away. Also, I think a velocity is calculated from the movement.

Turning on and off Is Kinematic when you start and stop moving it may do the trick.

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 T27M · Sep 22, 2012 at 08:35 PM 0
Share

Yup worked perfectly, Thanks.

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

10 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Can't get a laser working properly. 2 Answers

CharacterController had no enabled property 1 Answer

Creating a collider in a gameObject using only code 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