- Home /
Smooth movement like Lerp with Touch Input?
How can I get smooth movement like with this code?
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
//Movement
transform.Translate (Vector3.right * moveHorizontal * speed * Time.deltaTime);
transform.Translate (Vector3.forward * moveVertical * speed * Time.deltaTime);
That code smoothly moves my player when I press the arrow keys on my keyboard, but I don't know how to do it using a touch screen.
Like, the movement Vector3.Lerp does, but Lerp only moves the object from one point to another. I want it to keep moving endlessly until the player stops touching the button.
Here is my current code for the Touch Up-Button:
if(Physics.Raycast(ray, out hit) && hit.transform.tag == "UpButton")
{
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Began || theTouch.phase == TouchPhase.Stationary || theTouch.phase == TouchPhase.Moved)
{
transform.Translate (Vector3.forward * Time.deltaTime * speed);
}
}
}
This code moves the player forward when I press the Up Button on my touch screen, but it doesn't do so smoothly. When I stop touching the button, it stops immediately. I want it to stop slowly, like what Lerp does.
Thanks! :)
Answer by zero3growlithe · Jun 22, 2014 at 09:03 PM
The difference between "touch" controls and normal "keyboard" is that Unity discretly lerps the RAW input value it gets from keyboard to its target value thus making the movement "smooth" (you can control how fast should the input value change in Edit -> Project Settings -> Input -> Some key -> Gravity/Sensitivity)
So for touch controls you'll have to make your own code that will do the same, something like:
private float smoothInputVertical;
public float sensitivity = 5;
public float gravity = 3;
/*function name and some random code*/
if(Physics.Raycast(ray, out hit) && hit.transform.tag == "UpButton")
{
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Began || theTouch.phase == TouchPhase.Stationary || theTouch.phase == TouchPhase.Moved)
{
smoothInputVertical = Mathf.Lerp (smoothInputVertical, 1, Time.deltaTime*sensitivity);
transform.Translate (Vector3.forward * Time.deltaTime * speed * smoothInputVertical);
}
}
} else {
smoothInputVertical = Mathf.Lerp (smoothInputVertical, 0, Time.deltaTime*gravity);
}
(code not tested)
Answer by Chintan-Kansagara · Mar 16, 2017 at 09:12 AM
Hello friends. i am raycast to hit check and hit point to character movement in update but character not smoothly working. plz help me. my Code :-
void Update() { RaycastCheck(); } void RaycastCheck() { Vector3 mousePos = Input.mousePosition; mousePos.z = 10; Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);
RaycastHit2D[] hits;
hits = Physics2D.GetRayIntersectionAll(Camera.main.ScreenPointToRay(Input.mousePosition),100);
for(int i = 0; i < hits.Length ; i++)
{
if (hits != null /*&& hits[i] != null && hits[i].collider != null && hits[i].collider.name.Contains(balloontype.name)*/)
{
if(hits[i].collider.tag == "Hero")
{
PlayerHit = true;
}
if(hits[i].collider.tag == "Wall")
{
WallHit = true;
//Touch touch = Input.GetTouch(0);
//Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
//gameObjectTodrag.transform.position = Vector3.Lerp(gameObjectTodrag.transform.position, screenPos, Time.deltaTime);
//gameObjectTodrag.transform.position = Vector3.MoveTowards(gameObjectTodrag.transform.position,hits[i].point,Time.deltaTime * 5f);
}
else
{
WallHit = false;
}
//myHitList.Add(hits[i].collider.gameObject);
}
if(WallHit && PlayerHit == true)
{
//gameObjectTodrag.transform.position = hits[i].point;
//Player.transform.position = screenPos;
gameObjectTodrag.transform.position = Vector3.MoveTowards(gameObjectTodrag.transform.position,hits[i].point,Time.deltaTime * 50f);
}
}
}
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Using Lerp in a Constant Moving Object 1 Answer
How do I use lerp to have an instantiated gameobject lerp upwards? 1 Answer
Move from one position to another in x seconds 2 Answers
Vector3.lerp causes gameobject to move thousands of pixels away from where it's mean to go 1 Answer