Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by chrisPOD · Mar 09, 2011 at 09:21 PM · rigidbodyaxisplatformerpushz-axis

Restricting an object on its Zaxis.

Hello everyone i would like to modify Unitys PlatformerPushBodies script so that an object can not be pushed on its Z-axis.

This is Unitys Script and i want to change it so when i push the object it only goes left/right and up/down. Not turn and go off the sides because im working on a 2.5d Side Scroller. Thank You

// Script added to a player for it to be able to push rigidbodies around.

// How hard the player can push var pushPower = 0.5;

// Which layers the player can push // This is useful to make unpushable rigidbodies var pushLayers : LayerMask = -1;

// pointer to the player so we can get values from it quickly private var controller : PlatformerController;

function Start () { controller = GetComponent (PlatformerController); }

function OnControllerColliderHit (hit : ControllerColliderHit) { var body : Rigidbody = hit.collider.attachedRigidbody; // no rigidbody if (body == null || body.isKinematic) return;

 // Only push rigidbodies in the right layers
 var bodyLayerMask = 1 << body.gameObject.layer;
 if ((bodyLayerMask & pushLayers.value) == 0)
     return;

 // We dont want to push objects below us
 if (hit.moveDirection.y < -0.3) 
     return;

 // Calculate push direction from move direction, we only push objects to the sides
 // never up and down
 var pushDir = Vector3 (hit.moveDirection.x, 0, 0);

 // push with move speed but never more than walkspeed
 body.velocity = pushDir * pushPower * Mathf.Min (controller.GetSpeed (), controller.movement.walkSpeed);

}

Thank You ps. I am very new to scripting, ive been trying to script for only 2 weeks so this was sort of a complicated script for me.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Mar 09, 2011 at 10:59 PM

Check the Z axis on Freeze Position for the rigidbody constraints.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chrisPOD · Mar 10, 2011 at 02:19 AM 0
Share

is that real i looked it up on the Unity Scripting Reference they only had Rotation Freeze.

avatar image Eric5h5 · Mar 10, 2011 at 03:34 AM 0
Share

Obviously it's real, or I wouldn't have bothered to say anything about it. http://unity3d.com/support/documentation/ScriptReference/Rigidbody-constraints.html http://unity3d.com/support/documentation/ScriptReference/RigidbodyConstraints.html

avatar image
0

Answer by Freddie · Mar 09, 2011 at 10:12 PM

it's quite a dirty way of doing it, but this is the way i'd do it: you declare a variable:

var depth = 0.0;

to the editor, (put it outside any functions, right up at the top of the script)

then add:

body.position.z = depth;

right at the bottom of the script (but still inside the OnControllerColliderHit function)

then you can change the variable "depth" in the editor, and your object will stay at that Z co-ordinate

seeing as you're new to scripting i'm happy to explain it differently if you want; and if someone else has a better way of doing it, do tell.

hope that helped.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Platformer axis change 0 Answers

Rigidbodies push other rigdidbodies 1 Answer

Rigidbody Controller Jump 0 Answers

Aircraft Banking Rotation Problem 2 Answers

Rotation eulerangles not adjusting correctly... 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges