Ive been trying this for hours, using different combinations of colliders and such and for the life of me i cannot get a small sphere to stay inside a bigger sphere.
everything i try results in the smaller sphere instantly moving outside the bigger sphere and moving it somehwere.
the bigger sphere must not move by being moved by another object, and the smaller sphere has to stay inside the bigger one no matter what.
Just so I can understand what could help you, how are you applying force?
My reason for asking being, if you are simply moving the object using normal transforms, it will move through anything, even other colliders. You have to apply movement via physics in FixedUpdate() if you want the colliders to work properly
If you have Unity 3 (chances are you do because you upgraded), make sure your rigidbody has “Continuous” for it’s collision detection turned on. I have -YET- to see a rigidbody go through another mesh with this option turned on.
*Note: Continuous is only if it is the only thing receiving this kind of detection and not recommended when using it with multiple objects as this may slow down your processing speed
function FixedUpdate () {
transform.Translate(Cameracontrol.relativeMousePosition.x/5000, Cameracontrol.relativeMousePosition.y/5000, 0);
print (Cameracontrol.relativeMousePosition);
}
im adding the division by 5000 because anything else was just WAY too fast and the sphere popped off the screen before the game loaded. it was funny seeing how fast it was moving. but after dividing it, it moved sluggish and slow.
basically i was trying to lock that sphere to the mouse’s actual screen location.
NOT adding to the movement, but REPLACING its position with the position of the mouse.
(heres the cameracontrol script, note that both of these are stripped down versions of what they actually do)
var centerScreen : Vector2;
static var relativeMousePosition : Vector2;
function FixedUpdate() {
centerScreen = Vector2(Screen.width/2.0, Screen.height/2.0);
relativeMousePosition = Input.mousePosition - centerScreen;
//for (var child : Transform in transform) {
//child.position += Vector3.right * relativeMousePosition.x/200;
//child.position += Vector3.up * relativeMousePosition.y/200;
//}
//this stuff is what i was doing before the other script came to life. the object is no longer a child. i was just experimenting with the child position to see how that would go.
}
Instead of dividing by anything, you need to multiply by TIme.deltaTIme - this will get you a consistent movement. You can then multiply by a scalar to set a desired speed
thats the problem. i dont want it to move “per second”. I want it to move as far as the mouse moved in the direction that the mouse moved and have the mouse stop when it stops. all of this will be worked around with that other topic. I dont plan on using this 3d movement anymore as doing it in 2d and just representing the cursor movement into 3d values that i can use to rotate a camera with.
If youve played “room war” you would understand the exact movement i am looking for.
You’re getting the pixel difference, up above. The change in mouse position is in pixels, not game units, but you’re using pixel difference (which can be very large) to move an object in world space. You have to convert the mouse’s movement into world coordinates
Say your object can only move from -100 to +100 on any axis -
Surprised no one else has mentioned this yet but this really isn’t a physics problem. You should not be using colliders.
You want to contain a cursor to be within a circle. This is a much simpler problem that you can solve with some basic trig. Even if your targeting is in 3D space using 3D objects, it’s easier, faster and more reliable to contain an object within a bubble by clamping it’s position.
exactly! i was just using the 3d representation to demonstrate what i wanted. containing that cursor in that circle is what i have no idea on how to do…
lol well, if i get anything substantial in my endeavors with this game ill buy you a new laptop. youve been a great help.
now to get off my lazy butt and open up unity for the first time today (which as of yet i have to do)
then ill pretty much only need help on how to add the kongregate api to keep track of… something… i dunno. thinkin about adding
a ship that avoids you like crazy (not impossibly) and avoids hitting the city buildings. which i THINK i have a grasp of. i dunno. youll soon find out.
edit:
vincente that code there ALMOST works. not sure exactly whats going on, but the mouse stays inside a circle located to the top leftmost point of the screen AND only works when the actual mouse cursor is on the top rightmost part of the screen. once you move it anywhere else, the cursor just plays pingpong until it disappears.
ill try the other one you wrote up and see what happens. trying to find the error but it seems solid… hmm
Youre right, it does only limit it like youre saying. Its what Im wanting, the only thing we need to figure out now is to invert that Y axis somehow.
(i figured it out by wiggling the mouse around and saw the cursor bouncing around like it should. and sometimes got lucky and was able to keep it in the middle)
After playing with values for a while and seeing what happened, I got it to work. Its very confusing.
private var mousePos : Vector2;
var mouseTex : Texture2D;
var radiusRatio : float = 0.25; // how much of the screen to allow movement over (width)
[COLOR="red"]var relativeMousePosition : Vector2;[/COLOR]
var screenCenter : Vector2;
var radius : int;
function Update() {
radius = Screen.width * radiusRatio;
screenCenter = Vector2( Screen.width/2, Screen.height/2);
//Screen.lockCursor = true;
[COLOR="red"] relativeMousePosition = Input.mousePosition - screenCenter;[/COLOR]
[COLOR="red"]mousePos = Vector2(relativeMousePosition.x + Screen.width/2, -relativeMousePosition.y + Screen.height/2);[/COLOR]
if ( (mousePos - screenCenter).magnitude > radius ) {
mousePos = screenCenter + (mousePos-screenCenter).normalized * radius;
}
}
p.s. i like to call my variable before the update… just a personal pref… who knows if its faster or not.
now all i gotta do is figure out why locking the cursor breaks it…
It’s not slower to declare the variable before update, and it’s likely faster, so I’d leave it like that. I usually do that, too, I just didn’t think about it for some reason.