How do I get a sphere to collide INSIDE a bigger sphere.

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.

Create a sphere in a modeling program, duplicate it, scale the dupe down a small amount and flip its normals.

Collision only happens relative to object normals; if your sphere doesn’t have any pointing inwards, then nothing can collide with it from within it.

so i cant just create a sphere with the create tool and somehow flip the normals that way?

so create a sphere in a modeling program, and create another sphere in a modeling program with normals flipped?

(just trying to make it nice and easy and fast) i dont exactly know how to flip normals in say… 3ds

alright so i created a sphere and flipped the normals and imported that model. created a mesh collider since that seemed to be the only way around it.

i then made a normal sphere and put it inside that sphere.

well, it seems to work, but the small sphere barely resists against the inside of that sphere before popping outside of it.

how in the heck do i make that sphere stay inside no matter how much force i apply to it?

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 :slight_smile:

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

im using

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

For instance,

transform.position += transform.forward * TIme.deltaTime * 5.0;

That will move an object forward at a constant speed of 5.0 units per second.

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.

here ill upload it.

http://www.mediafire.com/file/egf54bowto1lg82/Room_War.rar

And if you go here youll see what I have so far.

http://www.kongregate.com/games/kingoftaurus/city-war

p.s. anyone who offers constructive help will recieve part of my winnings if i get any (not a bribe)

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 -

ratio = Vector2( 100.0 / Screen.width, 100.0 / Screen.height );
object.position += Vector2( mouseDelta.x * ratio.x, mouseDelta.y * ratio.y );

(I can’t run Unity on this laptop :()

room war is an exe made with darkbasic. it will run. hell, it will run on my ancient old intel celeron without a graphics card. :slight_smile:

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…

Hah, it almost runs xD
This laptop is the terrible.

But anwyhoo, that’s definitely a ‘constrain within radius’ problem.

private var mousePos : Vector2;
var mouseTex : Texture2D;
var radiusRatio : float = 0.25; // how much of the screen to allow movement over (width)

function Update() {
  var radius : int = Screen.width * radiusRatio;
  var screenCenter : Vector2 = Vector2( Screen.width * 0.5, Screen.height * 0.5 );
  Screen.lockCursor = true;

  mousePos += Input.mousePosition - screenCenter;
  if ( (mousePos - screenCenter).magnitude > radius ) {
    mousePos = (mousePos-screenCenter).normalized * radius;
  }
}

function OnGUI() {
  DrawTexCentered( mouseTex, mousePos );
}

function DrawTexCentered( t : Texture2D, p : Vector2 ) {
  GUI.DrawTexture( Rect( p.x-t.width * .5, p.y-t.height * .5, t.width, t.height ), t );
}

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

Guess I know who I’m voting for if I don’t get Unity back in time to make my own game. ^…^
Agh, I hate not being able to test my code. xD

This line right here:

mousePos = (mousePos-screenCenter).normalized * radius;

Is terrible. It should be:

mousePos = screenCenter + (mousePos-screenCenter).normalized * radius;

lol that partially fixed it. it now works backwards. in other words when you rotate the mouse clockwise, the cursor rotates counter-clockwise.

also along with the other post, the cursor needs to move inside the circle too. not just around

edit: ugh in the middle of typing my daughter kicked the power cord and i lost what i was just doing. lol. ill do it again

Yeah, GUI and screen Y coordinates are inverted from each other for some reason.

That shouldn’t be sticking… it only snaps cursor to limit if it’s outside, I thought.
Curse my inability to test.

Perhaps there’s something wrong with how it’s getting the mouse position?

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.

Glad you fixed it, though, and good luck. :slight_smile: