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 Robodude678 · Mar 14, 2014 at 06:23 AM · collisioncolliderpath

How to Prevent a Script From Overriding Colliders?

Ok so basically I have a gameobject that follows my cursor along with some stationary ground that when released from the cursor it will land on and obey physics. Everything works fine besides when I am holding it and my cursor passes through a piece of ground. This causes it to travel into the ground when I want it to obey the colliders that are on all of the objects and just follow it as best as it can.

This is my code:

 using UnityEngine;
 using System.Collections;
 
 public class CursorFollow : MonoBehaviour {
     public GameObject Cam;
     public int cursorSizeX;
     public int cursorSizeY;
     public Texture2D yourCursor;
     public Texture2D altCursor;
     public bool follow = false;
     public bool changeMouse = false;
     // Use this for initialization
     void Start () {
         Screen.showCursor = false;
     }
     
     // Update is called once per frame
     void Update () {
         //transform.position = Cam.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
 
         follow = Input.GetMouseButton (0);
 
         if (follow && changeMouse) {
             transform.position = Cam.camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
             rigidbody.velocity = new Vector3(0, 0, 0);
 
         }    
     
     }
 
         
     void OnMouseEnter () {
         changeMouse = true;
     
     }
 
     void OnMouseExit() {
         if (!follow) {
             changeMouse = false;
 
         }
     }
 
     void OnGUI() {
         //Screen.showCursor = !changeMouse;
         if (changeMouse) {
                         //EditorGUIUtility.AddCursorRect(new Rect(10, 10, 100, 100), MouseCursor.Link);    
                         //Cursor.SetCursor(MouseCursor.Link, Vector2.zero, CursorMode.ForceSoftware);
                         GUI.DrawTexture (new Rect (Event.current.mousePosition.x - cursorSizeX / 2, Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), altCursor);
 
                 } else {
         
                         GUI.DrawTexture (new Rect (Event.current.mousePosition.x - cursorSizeX / 2, Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), yourCursor);
                 }
     }
 }

If anyone could help it would be greatly appreciated! :)

Comment
Add comment · Show 5
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 getyour411 · Mar 14, 2014 at 06:27 AM 0
Share

Please take the time to format (all) your code properly.

I think transform.position is the problem, it ignores all colliders, collisions, etc.

avatar image Robodude678 · Mar 14, 2014 at 06:40 AM 0
Share

Sorry about the format, had to do it really quickly and honestly don't know how.

But anyway do you know how to make something work in the same way without transform.position?

avatar image getyour411 · Mar 14, 2014 at 06:57 AM 0
Share

Look at $$anonymous$$oveTowards, Translate (might have same issue w/ colliders, can't recall)

avatar image robertbu · Mar 14, 2014 at 07:42 AM 0
Share

You need to take a look standard DragRigidbody.js script. You can get it by:

 Assets > Import Package > Scripts

It only needs to go on one object. I would create a sample scene with a plane and some boxes. Attach the script to the camera or an empty game object, then play in the scene. This script likely isn't a whole solution, but it will give you some ideas on how you can drag an object in the scene and have it obey the colliders.

avatar image Robodude678 · Mar 14, 2014 at 08:13 PM 0
Share

I tried that but it still isn't following the colliders, do I have to tag anything in the scene or set layers?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · Mar 14, 2014 at 08:17 AM

Hi there

So the problem you're gonna hit here is that in its basic form, you are simply overiding the physics every frame when holding the object. So it doesn't matter whether the physics engine wants to resolve the collision - you're are overriding any attempts to do so!

This actually turns out to be a not-entirely-trivial issue in any game. Typically you have 2 options:

  • try to predict if putting the object where you want it to be would cause an intersection, and if so either refuse to move it or even try and guess a better place.

  • work with the physics engine constraints system to move your object

Generally, the 2nd option works better because you want something sensible to happen when dragging the object against something - for example pushing your object onto the floor then moving your mouse should drag the object along the floor.

So how to do this? Well whether its unity development or low level c++ on a console, I typically:

  • create an invisible kinetic rigid body that 'follows' the mouse cursor perfectly. This needs no collision whatsoever.

  • create a fixed joint between your invisible body and the body you're moving that tries to get them line them up in the position/orientation that matches how you grabbed it.

  • the joint can have 'maximum forces' it is allowed to apply, and breaking point. You can even play with things like spring joints for better effect

By using this 'ghost body' plus constraint approach you're effectively asking the physics to do the complicated maths for you! This means it'll not only give you good results, but because you're properly working with the physics, stuff like velocity and interaction with other dynamic objects will work!

Shout if you need a help with this as actual code, although there's plenty of examples online for 'creating joints at run time'.

Note: Your 'moving of the rigid body' should be done in FixedUpdate so it runs at the same speed as the physics!

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 Robodude678 · Mar 14, 2014 at 08:33 PM 0
Share

Hi wibble,

Thanks for that! That is a much better way than what I had previously! If you don't $$anonymous$$d though could you help me out on enabling and disabling the spring joint? Because I only want the cube to follow my cursor when I am clicking.

Cheers! :D

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

21 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

Related Questions

Trying to check area for game objects that collide with spawnpoints, then spawn an item where ever there is not a collision 0 Answers

Projectiles, their speed, and collisions 1 Answer

Jumping on top of an enemy problem 1 Answer

2D Platformer - Action When Key Is Pressed During Collision C# 0 Answers

How can i make a projectile attack travel through multiple enemies dealing damage to all of them? 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