Question by
Excision361 · Feb 03, 2019 at 01:45 AM ·
scripting problemmovementplayer
Help with simple player movement
Okay, so I have a script here that allows the player to move a game object around the world with their finger. My only problem is, if I were to use this same exact script on a 3d game object, the game object does not move at all. Why is that? Also, touchPos.x and touchPos.y is always a constant number no matter where my finger is placed on the screen. How do I go about fixing this script?
private float deltaX, deltaY;
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
deltaX = touchPos.x - transform.position.x;
deltaY = touchPos.y - transform.position.y;
break;
case TouchPhase.Moved:
rb.MovePosition(new Vector2(touchPos.x - deltaX, touchPos.y - deltaY));
break;
case TouchPhase.Ended:
rb.velocity = Vector2.zero;
break;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Player not moving in the right direction instantly 1 Answer
Hit a wall in my RTS movement controller. Issues with MoveToward and Coroutine logic. 0 Answers
Smooth/realistic crouching script in C#??? 0 Answers
how to move a gameobject based on a dice roll 1 Answer
How can I make the camera not overreach a limit of rotation one axis. 0 Answers