- Home /
Moving smoothly around the bounds of a circle
This is the image of what I'm trying to achieve, in a 3D space. I want the object to follow my finger within the green zone, but stay on the edge of the green zone if I move my finger outside of it. I have achieved this with the code below, but when moving my finger around the red zone a lot of jitters and clipping occurs as the object keeps snapping back within it's bounds. My main camera is attached to the moving object so it's important that I eliminate the jitters. How can I smooth this out?
public class Player : MonoBehaviour
{
public Camera movementCam;
readonly float radius = 0.45f;
readonly float speed = 3f;
Ray firstTouchPos;
Vector2 playerPos;
void Update()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
float distance = Vector3.Distance(transform.position, Vector3.zero);
transform.position = distance >= radius ? (Vector3)(direction.normalized * radius) : (Vector3)(playerPos + direction * speed);
}
}
}
Answer by Cornelis-de-Jager · Jan 31, 2019 at 09:40 PM
You can achieve this by using Lerp or Slerp. See example below
public Camera movementCam;
readonly float radius = 0.45f;
readonly float speed = 3f;
Ray firstTouchPos;
Vector2 playerPos;
void Update()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
float distance = Vector3.Distance(transform.position, Vector3.zero);
var targetPosition = distance >= radius ? (Vector3)(direction.normalized * radius) : (Vector3)(playerPos + direction * speed);
transform.position = Vector3.Lerp (transform.position, targetPosition, 1f);
}
}
However, I would recommend you put the slerp function outside of the MouseButtonDown. That way you can add a smoother following/delay
readonly float radius = 0.45f;
readonly float speed = 3f;
Ray firstTouchPos;
Vector2 playerPos;
Vector3 targetPosition;
float followDelay = 2f;
void Update()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
float distance = Vector3.Distance(transform.position, Vector3.zero);
targetPosition = distance >= radius ? (Vector3)(direction.normalized * radius) : (Vector3)(playerPos + direction * speed);
}
transform.position = Vector3.Lerp (transform.position, targetPosition, followDelay);
}
Thank you @Cornelis-de-Jager for the response. Adding a Lerp is definitely a good idea and will smooth out the movement of the camera while in the bounds, but the jitters I'm seeing are caused when holding my finger in the red zone out of the players circle bounds. Ins$$anonymous$$d of being "stuck" in the bounds the player is trying to continue and then being positioned back within the bounds, causing jitters. I'm looking for a way to limit the movement of the player within the bounds without having to reset it's position.
Your answer
Follow this Question
Related Questions
How to select one character and play it's animation? 1 Answer
Help with player movement and jumping 0 Answers
how to rotate velocity/ tying movement to camera 0 Answers
Unity Movement 1 Answer
Panning and Rotating a Camera 0 Answers