Hey guys!
I have code for move object to mouse X, but this code does not count the distance(the object follows straight in the mouse)
How could I get their kind of controls? Where the player moves positions between offsets instead of lerping to your finger position?
Can you please help me?
private void HorizontalMovement() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100)) { transform.position = Vector3.Lerp(transform.position, new Vector3(hit.point.x, transform.position.y, transform.position.z), speed * Time.deltaTime); } }
Where you have put speed*Time.deltaTime needs to be a float between 0 - 1, 0 being the start point and 1 being the end point. Lerps also have to be repeated/updated to get movement, here i’m using a ‘for loop’ to complete the lerp.
Vector3 mousePos;
if (Physics.Raycast(ray, out hit, 100)) {
StartCoroutine(MoveToPosition);
mousePos = new Vector3(hit.point.x, transform.position.y, transform.position.z)
}
IEnumerator MoveToPosition()
{
for(float currentPos = 0; currentPos < 1; currentPos += speed * Time.deltaTime)
{
transform.position = Vector3.Lerp(transform.position, mousePos, currentPos);
yield return WaitForFixedUpdate;
}
}
Just edited, I noticed it was a new vector3 so I stored it as a vector3 variable.
Unfortunately in this case the object does not move yours.