- Home /
Mouse Controlled object moving inside a circle.
Ok so im trying to make an object that is controlled by the mouse but is limited to a circle. the purple dot is the object and as you can se it still needs to move even though the mouse exits the circle (the mouse should not be visible in-game) And also the whole thing needs to be a child of another object so it moves relative to its parents position. But so far ive had no luck at achieving this.
I hope you understood my problem and are able to help me Thank you :)
Answer by Scribe · Nov 12, 2012 at 09:13 PM
I wrote this:
var Parent : Transform;
var Obj : Transform;
var Radius = 5;
var Dist : float;
var MousePos : Vector3;
var ScreenMouse : Vector3;
var MouseOffset : Vector3;
function Update() {
MousePos = Input.mousePosition;
ScreenMouse = camera.main.ScreenToWorldPoint(Vector3(MousePos.x, MousePos.y, Obj.position.z-camera.main.transform.position.z));
MouseOffset = ScreenMouse - Parent.position;
Obj.position.x = ScreenMouse.x;
Obj.position.y = ScreenMouse.y;
Dist = Vector2.Distance(Vector2(Obj.position.x, Obj.position.y), Vector2(Parent.position.x, Parent.position.y));
if(Dist > Radius){
var norm = MouseOffset.normalized;
Obj.position.x = norm.x*Radius + Parent.position.x;
Obj.position.y = norm.y*Radius + Parent.position.y;
}
}
However Jamie suggested much the same thing and got in there first! Others might find this useful in the future however so I thought I'd share it.
Also might help if you can't work out how to utilise Jamie's code.
Scribe
I can't seem to get it fully working the object is moving as it should almost my only problem is that as soon i test it the axis point moves to the very bottom. And also the object doesnt follow when the parent moves.
I don't quite understand what you mean by axis point, however I have fixed the parent movement problem. If you can explain the other problem a little more I can try to fix that too. It could be as I am only changing the x and y position of the objects? what axis are you using, from what view is your camera pointing?
It was the exact same thing i was working with (the parent.position) but the problem is that there is a small offset between the parent and the object on the z axis, so when you plus the parents position it moves away in the z axis.
and i keep getting this error: "Operator '+' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'float'." I dont have enough basic knowledge of how this works to fix it.
Sorry about that, changed some of the code in the if statement so it should not be effected at all by the z position. "Operator '+' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'float'." is pretty much what it says, you can't write Vector3(a, b, c) + d... it doesn't know what to add d to. Though I pressume this came up whilst you were trying to fix your code as there should not be any errors in the above code now.
Found some weird glitches which were a result of me forgetting to find an offset of the mouse position from the parent. fixed that now aswell!
Goodluck, hopefully it all works now, Scribe
Answer by JamieFristrom · Nov 12, 2012 at 08:54 PM
It depends a lot on what you're using to move your purple dot, but after the move happens you can constrain it within the circle.
(This is pseudocode - rewriting in js or csharp up to you.)
In world space:
if( Vector3.Distance( position, origin ) > maxDistance )
newPosition = (position-origin).normalized * maxDistance + origin;
Or, if your purple dot is parented to the center, might look something like this:
if( position.magnitude > maxDistance )
newPosition = position.normalized * maxDistance;
(Also, I recommend boning up on your vector math if you're going to be making a 2D game - time spent now will save you time later.)
Sure, do you have any recommendations of where to learn about this? Edit: just one thing to take into account, and i feel stupid not having mentioned it. It's '2D' as in its seen from the side but everything is modelled in 3D to get some depth.
Essential $$anonymous$$athematics for Games is a good one.
Your answer
Follow this Question
Related Questions
Moving a game piece with the mouse 1 Answer
Dragging movement Speed 1 Answer
Move player in circle around center 0 Answers
Runescape Movement and camera rotation 2 Answers