Question by
NickKroiss · Apr 04, 2020 at 09:28 AM ·
playerplayer movement
There has to be a better way to move the player along the X and Y axis restricted by radius
What I have works, its just a bit clunky, the game will move X and Y but if you move quickly with your mouse it will snap out of bounds for a second and I feel like its not completely optimized for a 3D mobile game.
public GameObject centerPt;
private float speed = 8.0f;
void Update()
{
float radius = 3.5f;
Vector3 centerPosition = centerPt.transform.position;
Vector3 plyLocation = transform.position;
float distance = Vector3.Distance(plyLocation, centerPosition);
Debug.Log("Distance:" + distance);
if (distance < radius)
{
var x = Input.GetAxis("Mouse X") * Time.deltaTime * speed;
var y = Input.GetAxis("Mouse Y") * Time.deltaTime * speed;
if((radius - 1) < distance){
transform.Translate(-x, -y, 0);
} else {
transform.Translate(x, y, 0);
}
} else {
transform.position = centerPt.transform.position;
}
}
Comment
Your answer

Follow this Question
Related Questions
How to add a jump key 2 Answers
Any ideas in relate to a cube rolling? thank you! 0 Answers
How to make better player movement 0 Answers
(NEED HELP!) Camera controlled by mouse Y, player controlled by mouse X 2 Answers
Ground check BoxCast is detecting when player hits bottom of platform. UNITY 2D C# 0 Answers