Restricting Movement in 2D

Hello

I’m sorry if this is a re-post but I have looked through the forums and have been unable to find a solution to my problem. I am trying to restrict an object to move only in the X-Y plane, as the game is a simple 2D scrolling. I have attached a Rigidbody component and the object moves from the left edge of the screen to the right, with a hidden cube placed in the centre to stop it passing off screen. The input is from the mouse not the keyboard.

I have modified a script I found on the wiki site

function FixedUpdate ()
{

	// Get the input and set variables for it
	horizontal = -Input.GetAxis("Mouse X"); 
	vertical = Input.GetAxis("Mouse Y");

	horizontal *= force;
	vertical *= force;
	

	if(rigidbody.velocity.magnitude < maxspeed))
	{
		//cancel out the forces in the Z direction
		// * It is relative to the camera
		// * we remove the z component because we really want to apply forces only on the 2D plane
		var up = transform.TransformDirection(Vector3.up);
		up.z = 0;
		rigidbody.AddForce (up * vertical); 
		
	
		var right = transform.TransformDirection(Vector3.right);
		right.z = 0;
		rigidbody.AddForce (right * horizontal);

                Debug.Log(transform.rotation);


	} 
}

When I check the transform using the debug I can still see values in the Z axis. A quick fix was to modify the Camera Mouselook C# script, clamp the rotation of my Rigidbody object. But the movement is somewhat jerky as I’m sure the two scripts are fighting each other…

    rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    rotationY = ClampAngle (rotationY, minimumY, maximumY);

    Quaternion yQuaternion = Quaternion.AxisAngle (Vector3.left, Mathf.Deg2Rad * rotationY);
    transform.localRotation = originalRotation * yQuaternion;

I’m sure I’m making a mess of what should be a simpler solution. Any help greatly appreciated.

You can add this at the end of the script:
transform.position = new Vector3(transform.position.x, 0.0f, transform.position.z);

Rigidbody should be used only for physics simulation, if you need to move a object with the mouse it’s easier to add vectors to transform.position

We had a pretty good experience using a ConfigurableJoint attached to each object that should only move in two dimensions - just need to attach the component, set ZMotion to “Locked,” and then AngularXMotion and AngularYMotion to “Locked” as well (since we also only wanted rotations about the z axis.

You also want it to be configured in world space. Or just put this script in the Assets/Editor folder and use the 2D menu it makes.

@MenuItem ("2D/Move Onto 2D Plane ^2")
static function MoveOnto2DPlane () {
	for (var transform in Selection.transforms) {
		transform.position.z = 0;
	}
}

@MenuItem ("2D/Move Onto 2D Plane ^2", true)
static function ValidateMoveOnto2DPlane () {
	return (Selection.activeTransform != null);
}

@MenuItem ("2D/Make Selection 2D Rigidbody")
static function MakeSelection2DRigidbody () {
	MoveOnto2DPlane();
	
	for (var transform in Selection.transforms) {
		var rigidbody : Rigidbody = transform.GetComponent(Rigidbody);
		
		if (!rigidbody)
			transform.gameObject.AddComponent(Rigidbody);
		
		var configurableJoint : ConfigurableJoint = transform.GetComponent(ConfigurableJoint);
		
		if (!configurableJoint)
			configurableJoint = transform.gameObject.AddComponent(ConfigurableJoint);
		
		configurableJoint.configuredInWorldSpace = true;
		configurableJoint.xMotion = ConfigurableJointMotion.Free;
		configurableJoint.yMotion = ConfigurableJointMotion.Free;
		configurableJoint.zMotion = ConfigurableJointMotion.Locked;
		configurableJoint.angularXMotion = ConfigurableJointMotion.Locked;
		configurableJoint.angularYMotion = ConfigurableJointMotion.Locked;
		configurableJoint.angularZMotion = ConfigurableJointMotion.Free;
	}	
}

@MenuItem ("2D/Make Selection 2D Rigidbody", true)
static function ValidateMakeSelection2DRigidbody () {
	return (Selection.activeTransform != null);
}

Cheers,
-Jon

Jon, saved as what … “2D/Move Onto 2D Plane.cs” ??

It’s Jon. 8) I have mine saved as Assets/Editor/TwoDHelper.js .

Cheers,
-Jon

I knew that of course. Just bad typing today.

I’m trying to do something similar but having issues.

I’m trying to allow an object to rotate in all three dimensions but not move along the z plane. I borrowed some of the code here as I was also getting jerking motion previously.

How can I throw out movement along one plane but allow all the other physics to continue?

Take a look at the script I posted above. Just modify which axis are locked and free to what you want.

-Jon