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 Chris 35 · Mar 20, 2011 at 07:47 AM · javascriptrigidbody

Rigidbody Collision Help

im using the 2D platformerController in unity and it comes with a PushBodiesScript which allows the Player to push rigidboies around, but only side to side. what would i need to add so the player can push objects up and down? how would i do this?

// 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, hit.moveDirection.z); Original side to side // var pushDirY = Vector3 (hit.moveDirection.y, 0, hit.moveDirection.z); up and down but side to side dosent work var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);

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

}

i want to know what do i need to add so the player can push objects up and down?

how would i do this?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Meltdown · Mar 20, 2011 at 09:40 AM

You're better off trying to first understand 'what' the code you have written does, then it will be much easier to know how to push products in different directions.

var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);

pushDir takes a 3 dimensional vector. With the x component being I assume the direction of x of your player, the y component being 0 (meaning it stays at the same height), and z being the z movement of your character.

You simply need to understand what a vector is in 3d space. To push an object up or down, you need to manipulate the y component of the vector. You currently have it as 0, meaning the object will never go up or down. So play around editing this value and the pushDir will begin pushing objects up and down.

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 Chris 35 · Mar 20, 2011 at 09:48 AM 0
Share

i tried this var pushDir = Vector3 (hit.moveDirection.x, hit.moveDirection.y, hit.moveDirection.z);

but it still doesnt work ?

avatar image Meltdown · Mar 20, 2011 at 04:25 PM 0
Share

Try using RigidBody.AddForce on the object you are pushing ins$$anonymous$$d of setting the velocity.

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I test if car is grounded to stop it from driving in mid-air? 2 Answers

Unity3D Ai wont shoot 1 Answer


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