- Home /
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. :)
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.
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
Follow this Question
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