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 Ecnelis · Jul 06, 2013 at 12:30 AM · mousetransform.positionfollowlocation

Update position of an object to match a high velocity object and still allow OnMouseOver()

Hey, hopefully an easy question to answer. I have an object (textItem) that needs to follow another object (planeItem). I can achieve this no problem using the following code in Update():

     public static void AlignWithPlane(GameObject textItem, GameObject planeItem, float yOffset = 0.0f, float xOffset = -0.9f, float zOffset = -0.05f)
     {
         var v3Offset = new Vector3(xOffset, yOffset, zOffset);
         textItem.transform.position = planeItem.transform.TransformPoint(v3Offset);

         textItem.transform.rotation = planeItem.transform.rotation;
     }

However OnMouse* events are not triggered for the textItem when the planeItem is travelling at a high velocity. At slower speeds it is the same but intermittent.

If I understand correctly, the update code is slowly behind the planeItem so while the visual representation is fine (it's drawn right), the actual collider hasn't caught up yet?

Is there a way to translate the position of the textItem to the position of the planeItem both with movement and zero movement?

NOTE: I am bound by the rule of not being able to parent the textItem to the planeItem. This is unconditional for the scenario.

I would really appreciate any help. :)

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 DecipherOne · Jul 06, 2013 at 01:33 AM

Here's a script that should help you. You'll need to adjust it for all three axis however.

using UnityEngine; using System.Collections; public class FollowObject : MonoBehaviour { //Holds the vectors for the objects translation. Vector3 ObjTrans; //Vector3 holding new translation coords. public float ObjSpeed; //The speed at which the object moves. public Transform Object; //The target object for the camera. public Vector3 Offset; //An offset for the Object from the target object. public Vector2 NegBounds; //Bounding volumes to signal when the Object should be updated. public Vector2 PosBounds; //Bounding volumes to signal when the Object should be updated. bool update_x=false; //Signals when the Object should be updated. bool update_y=false; // Use this for initialization void Start () { ObjTrans.x = this.transform.position.x; ObjTrans.y = this.transform.position.y; ObjTrans.z = this.transform.position.z; } void Update() { UpdateBoundingVolumes(); //Check to see if our object is outside of the follow bounding volume. if(Object.position.x<NegBounds.x||Object.position.x>PosBounds.x) { update_x=true; } else { update_x=false; } if(Object.position.y<NegBounds.y||Object.position.y>PosBounds.y) { update_y=true; } else { update_y=false; } Follow(); } public void Follow() //!< Sets the transform for the Object to follow another specified object on the y axis. Takes a reference to said object. Must be updated in a scene loop. { if(update_x && update_y) { ObjTrans = new Vector3(Object.position.x,Object.position.y,Offset.z); } else if(update_x && !update_y) { ObjTrans = new Vector3(Object.position.x,this.transform.position.y,Offset.z); } else if(!update_x && update_y) { ObjTrans = new Vector3(this.transform.position.x,Object.position.y,Offset.z); } transform.position= Vector3.Lerp(transform.position,ObjTrans,Time.deltaTime*ObjSpeed); } void UpdateBoundingVolumes() { NegBounds.x = transform.position.x - Offset.x; NegBounds.y = transform.position.y - Offset.y; PosBounds.x = transform.position.x + Offset.x; PosBounds.y = transform.position.y + Offset.y; }

Hope it helps. Copy this, save it, put it in your project. Add it as a component to the object you want to be following. Assign the other object follow in the editor.

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 Ecnelis · Jul 06, 2013 at 06:55 AM 0
Share

Thank you very much! After playing around with your code I ended up only needing a part of it to work in this scenario (Vector3.Lerp & Speed).

     public static void AlignWithPlane(GameObject textItem, GameObject planeItem, float yOffset = 0.0f, float xOffset = -0.9f, float zOffset = -0.05f)
     {
         var v3Offset = new Vector3(xOffset, yOffset, zOffset);
         
         textItem.transform.position = Vector3.Lerp(textItem.transform.position, planeItem.transform.TransformPoint(v3Offset),Time.deltaTime* fSpeed);
         
         textItem.transform.rotation = planeItem.transform.rotation;
     }

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

16 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

Related Questions

can i let the sphere follow a point(x.y. or.z) on plane? 1 Answer

Want to move object slowly to where the mouse clicks? 1 Answer

Attach an object to the mouse cursor? 0 Answers

How to draw a small circle on scene and moving it wherever mouse cursor moves 2 Answers

Show object location with arrow 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