Search Unity

Restricting Movement in 2D

Discussion in 'Scripting' started by jack-jack, Nov 16, 2007.

  1. jack-jack

    jack-jack

    Joined:
    Oct 11, 2007
    Posts:
    9
    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

    Code (csharp):
    1.  
    2.  
    3. function FixedUpdate ()
    4. {
    5.  
    6.     // Get the input and set variables for it
    7.     horizontal = -Input.GetAxis("Mouse X");
    8.     vertical = Input.GetAxis("Mouse Y");
    9.  
    10.     horizontal *= force;
    11.     vertical *= force;
    12.    
    13.  
    14.     if(rigidbody.velocity.magnitude < maxspeed))
    15.     {
    16.         //cancel out the forces in the Z direction
    17.         // * It is relative to the camera
    18.         // * we remove the z component because we really want to apply forces only on the 2D plane
    19.         var up = transform.TransformDirection(Vector3.up);
    20.         up.z = 0;
    21.         rigidbody.AddForce (up * vertical);
    22.        
    23.    
    24.         var right = transform.TransformDirection(Vector3.right);
    25.         right.z = 0;
    26.         rigidbody.AddForce (right * horizontal);
    27.  
    28.                 Debug.Log(transform.rotation);
    29.  
    30.  
    31.     }
    32. }
    33.  
    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..

    Code (csharp):
    1.  
    2.     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    3.     rotationY = ClampAngle (rotationY, minimumY, maximumY);
    4.  
    5.     Quaternion yQuaternion = Quaternion.AxisAngle (Vector3.left, Mathf.Deg2Rad * rotationY);
    6.     transform.localRotation = originalRotation * yQuaternion;
    7.  
    8.  
    I'm sure I'm making a mess of what should be a simpler solution. Any help greatly appreciated.
     
  2. Lka

    Lka

    Joined:
    Aug 6, 2006
    Posts:
    297
    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
     
  3. biphenyl

    biphenyl

    Joined:
    Oct 16, 2007
    Posts:
    29
    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.
     
  4. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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.

    Code (csharp):
    1. @MenuItem ("2D/Move Onto 2D Plane ^2")
    2. static function MoveOnto2DPlane () {
    3.     for (var transform in Selection.transforms) {
    4.         transform.position.z = 0;
    5.     }
    6. }
    7.  
    8. @MenuItem ("2D/Move Onto 2D Plane ^2", true)
    9. static function ValidateMoveOnto2DPlane () {
    10.     return (Selection.activeTransform != null);
    11. }
    12.  
    13. @MenuItem ("2D/Make Selection 2D Rigidbody")
    14. static function MakeSelection2DRigidbody () {
    15.     MoveOnto2DPlane();
    16.    
    17.     for (var transform in Selection.transforms) {
    18.         var rigidbody : Rigidbody = transform.GetComponent(Rigidbody);
    19.        
    20.         if (!rigidbody)
    21.             transform.gameObject.AddComponent(Rigidbody);
    22.        
    23.         var configurableJoint : ConfigurableJoint = transform.GetComponent(ConfigurableJoint);
    24.        
    25.         if (!configurableJoint)
    26.             configurableJoint = transform.gameObject.AddComponent(ConfigurableJoint);
    27.        
    28.         configurableJoint.configuredInWorldSpace = true;
    29.         configurableJoint.xMotion = ConfigurableJointMotion.Free;
    30.         configurableJoint.yMotion = ConfigurableJointMotion.Free;
    31.         configurableJoint.zMotion = ConfigurableJointMotion.Locked;
    32.         configurableJoint.angularXMotion = ConfigurableJointMotion.Locked;
    33.         configurableJoint.angularYMotion = ConfigurableJointMotion.Locked;
    34.         configurableJoint.angularZMotion = ConfigurableJointMotion.Free;
    35.     }  
    36. }
    37.  
    38. @MenuItem ("2D/Make Selection 2D Rigidbody", true)
    39. static function ValidateMakeSelection2DRigidbody () {
    40.     return (Selection.activeTransform != null);
    41. }
    42.  
    Cheers,
    -Jon
     
  5. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Jon, saved as what ... "2D/Move Onto 2D Plane.cs" ??
     
  6. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    It's Jon. 8) I have mine saved as Assets/Editor/TwoDHelper.js .

    Cheers,
    -Jon
     
  7. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    I knew that of course. Just bad typing today.
     
  8. spawrks

    spawrks

    Joined:
    Nov 16, 2007
    Posts:
    89
    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?
     
  9. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Take a look at the script I posted above. Just modify which axis are locked and free to what you want.

    -Jon