- Home /
Move a rigidbody to touch position, with velocity or trackable parameters
I want to move a splice, it resembles a bat as gameobject.
Normally I use this code to change the position of the "bats "
if (Input.GetKey(KeyCode.Z))
{
rb.velocity = new Vector2(0f, sensitivityP1);
}
Because I can track the velocity to "controll" other game objects such as my ball with this code ;
if (Bat_1.GetComponent<Rigidbody2D>().velocity.y > 0.1f)
{
rb.velocity = new Vector2(x,y);
}
Now I've switched platforms (from PC to mobile) and the controls are no longer keys or buttons but touch positions.
Getting the bat to move or to "teleport" to the position on the screen was pretty easy with this code ;
position = new Vector2(Input.GetTouch(0).position.x, 0f);
pos = Camera.main.ScreenToWorldPoint(position);
rb.transform.position = new Vector2(pos.x, -4.5f);
But the problem is transform or moveposition no longer has a "trackable parameter" such as velocity. Which I can use to controll my ball in the game.
The purpose is to be able to interact with the ball which hits the bat and if the velocity is positive it would go right , if it was negative the ball would go left. This was pretty easy by using buttons.
Does anybody has any idea on how I can make the bats ( rigidbody's) move to the correct position on screen but still be able to use the code to interact with the gameobjects such as a Ball?
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
I can't jump sometimes 1 Answer
Camera rotation around player while following. 6 Answers
How do I control the camera position (in between borders)? 1 Answer
rigidbody.velocity is queued for processing after rigidbody.position 0 Answers