- Home /
How to clamp ovally?
Hi. First of all, forgive me if my english isn't the best you've ever seen, I'll try to do my best. I started unity a few weeks ago and I'm currently working on a kind of 2d catapult. I was wondering how to clamp the position of an object (here the paddle) ovally.
This is my scene
and I've already done this but I think that it's much more complexe considering that what I want do looks like a 2d oval joystick
I hope I've been clear enough.
I don't quite understand what is wrong with your current solution. Is it that the current system works by limiting point3 in relation to points 1 and 2, and you'd want a paddle that's tied from both ends?
I'd want the paddle to stay close from each point, drawing an ovally shape. For me, the most difficult part is to clamp the whole thing and to make the paddle follow the pointer/finger even if it's outside of the limit, like with a 2d joystick.
Answer by fafase · Jun 08, 2014 at 08:42 AM
I understand you want to clamp the position of the pad around the two points and since you have two points, it creates some kind of oval shape (it is actually more of a 8 shape, if I get it right).
You need to first check which of the two objects you are closer to, and then clamp the distance within a radius:
Transform closest;
Vector2 dist1 = Vector2.Distance(p1.position, transform. position);
Vector2 dist2 = Vector2.Distance(p2.position, transform. position);
if (dist1 < dist2)closest = dist1;
else closest = dist2;
// Clamp your value using closest.
I tried to clamp with Clamp$$anonymous$$agnitude but I think that there is a problem somewhere. Also, if what I want te do is a kind of slingshot, I think that it's better to use farthest.
Your answer
Follow this Question
Related Questions
C# Clamp joystick rotation 0 Answers
Joystick Controller Problem 0 Answers
Show Joystick on the left part of the screen only 1 Answer
How to get RenderTexture non-square? 2 Answers
door script problems 2 Answers