- Home /
Drag an object which camera follows
When you drag an object(http://answers.unity3d.com/questions/566327/drag-object-relative-to-camera.html) , while the program lets camera follow the object (by making the camera child object of the object or a script that follows the transform of the object), the dragged object moves in abruptly quickly. Is there any methods which allow the dragged objects is followed by the camera in normal way? I tried to limit maximum movement of the camera by using various ways such as Mathf.MoveTowards etc.. but no success. Thanks
I basically want to make a Side Scrolling games like $$anonymous$$ario but left and right movement by dragging by finger/mouse
$$anonymous$$oveTowards should do what you want. Look here. I would like to see what your attempt with $$anonymous$$oveTowards was so I can tell you what you did wrong.
Answer by peter_kes · Oct 07, 2014 at 11:04 AM
For the dragged Object (Could be more sofisticated though):
Vector3 initialPos; Vector3 curPos; float speed = 3f;
void Update () {
if(Input.GetMouseButton(0)){
curPos = Input.mousePosition;
}
}
void OnMouseDown() {
initialPos = Input.mousePosition;
}
void OnMouseDrag() {
if(curPos.x != initialPos.x )
{
transform.position = new Vector3(transform.position.x +(curPos.x - initialPos.x) * Time.deltaTime * speed, transform.position.y, transform.position.z);
}
}
For the camera: public Transform target; public float smoothTime = 0.3F; private Vector3 velocity = Vector3.zero;
void Start () {
transform.position = new Vector3 (target.transform.position.x,
target.transform.position.y,
transform.position.z);
}
void Update () {
transform.position = Vector3.SmoothDamp(transform.position, target.position, ref velocity, smoothTime);
transform.position = new Vector3(transform.position.x, the y position that it should stay as this is horizontal scrolling, -10f);
}
Answer by Andres-Fernandez · Oct 06, 2014 at 01:28 PM
Instead of moving the object directly with the finger drag, move an invisible mock object instead and then make the object follow that mock object's position using SmoothDamp, or lerp, or anything similar. When adding smoothness to movement (either to a camera or any other object that doesn't need to be time controlled) I prefer using the SmoothDamp option.
[Edit] Unless the object has a speed parameter (i.e. time controlled). Then use lerp instead, it's easier to tweak.
thank you for the answer. The camera movement using SmothDamp is very smooth to follow the object. The actual issue was not that though, but I found out that because $$anonymous$$ouseInput/Touch point is first calculated into unity world position using Camera.main.ScreenPointToRay. Camera position also moves along with the dragged object in my program, and the problem is that every dragging position is calculated in according to the moving Camera position due to the Camera.main... the dragged object moved too much. so I made it so that drag is done without using Camera.main.ScreenPo... .
Did you solve it? Can you post how you did it? It may help others with the same problem (and I'm curious about it).