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 Gerlon · Apr 05, 2014 at 10:51 PM · transformtransform.position

Attaching Object to mouse

Hello,

I'm working on a RTS and I have some trouble getting the object move smooth. It's following the mouse correctly but very jumpy at some point.

 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             Physics.Raycast (ray, out hit, Mathf.Infinity);
 
             if(hit.collider.gameObject.tag == "Ground"){
                 attachedObject.transform.position = new Vector3(hit.point.x,hit.point.y+3f,hit.point.z);
                 
                 hittedGround = hit;
 
             }else{
                 if(hit.collider.gameObject.tag == "layout" && transform.position!=hit.point){
                     attachedObject.transform.position = new Vector3(hit.point.x,hittedGround.point.y+3f,hit.point.z);
                 }
             }

I'm using a Raycast to get the correct "y" of the terrain and saving that to be able when I hover over the object that I can still move it with the same "y". But it seems between hitting the terrain and the object its a bit jumpy, like it gets moved to the position and after that moved to the correct "y". It's just an annoying effect. Maybe someone can help me.

Comment
Add comment · Show 7
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 sixEight · Apr 06, 2014 at 12:00 AM 0
Share

Are you using rigidbodies, and if so, are you in fixedupdate?

avatar image Gerlon · Apr 06, 2014 at 12:12 AM 0
Share

When i posted that i didn't use Rigidbodies and I used the Update. At the moment I attached a Rigidbody to be able to check for collisions and block the placement of the object. In the FixedUpdate the effect is even stronger. As long as Iam on the Object it seems fine, it is just when i move my mouse faster and get off and hit the ground again.

avatar image Gerlon · Apr 06, 2014 at 01:01 AM 0
Share

If I use it without a Rigidbody and Collider it is working a lot better, just checking will get a bit more complicated.

avatar image getyour411 · Apr 06, 2014 at 01:21 AM 0
Share

Does "layout" gameObject and "Ground" gameObject often have different Y values?

avatar image Gerlon · Apr 06, 2014 at 01:46 AM 0
Share

It was even jumping on flat ground. And layout has always the Y from the ground before. But it is updating a lot if u mean that. I'm not sure if my approach is good in any way, still tinkering a lot.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by NecrosDk · Apr 06, 2014 at 05:09 AM

I would do it so that when you click the object initially you turn on a bool in a script attached to the object, that defines that it has been picked up. Then in that seperate script I would say if it is picked up, make the position = Input.mousePosition. Right now it looks like you're raycasting it even when it's already picked up, which means if you move the mouse too quickly during one frame, it will fall out of the raycast? Remember also to make sure that the object doesn't collide with anything else in the scene if it's not supposed to do that.

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 Gerlon · Apr 06, 2014 at 09:52 AM 0
Share

Thanks for the response, but since I have some pla$$anonymous$$us as well I have to check at some point for a new Y. I just removed the Collider for now which fixed it since Iam now hitting the ground mainly and I made sure that I filter some obstacles like "trees" where the cast breaks and jumps the object. But now I have to do quite some Raycasting to check if obstacles are underneath it or does someone have a better way of doing that ? I just know Physics.OverleapSphere which is quite performace heavy when called a lot and I think Im done with Colliders for that attached object.

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

23 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

Related Questions

fixed distance for Vector3.moveTowards ? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Move Objects arranged in Circle Forward 1 Answer

Find transform.position of Another Game Object 1 Answer

Update transform component from server every frame 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