When you drag an object(Drag object relative to camera - Unity Answers) , 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
2 Answers
2For 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);
}
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 MouseInput/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... .
– peter_kesDid you solve it? Can you post how you did it? It may help others with the same problem (and I'm curious about it).
– Andres-Fernandez
I basically want to make a Side Scrolling games like Mario but left and right movement by dragging by finger/mouse
– peter_kesMoveTowards should do what you want. Look [here][1]. I would like to see what your attempt with MoveTowards was so I can tell you what you did wrong. [1]: http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
– KayelGee