Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 22, 2016 at 02:42 PM by TheAngerSpecialist for the following reason:

The question is answered, right answer was accepted. I figured it out myself...

avatar image
0
Question by TheAngerSpecialist · Oct 22, 2016 at 08:56 AM · c#transform2d gamerigidbody2dtop down

2D Game: Best way to set player's Z?

I'm making a top down 2D game with jump mechanics(graphics are going to look like Chrono Trigger or Terranigma if you'd like to sort of see what I'm seeing).

I've decided to use the z value of various objects to control their height. When the player jumps, the Sprite moves along the LocalY axis (this is an animation, basically.) The rigidbody2D controlling the Sprite stays in its shadow, directly underneath. I've got the Sprite moving up and down and following the controller correctly, but I want to copy its LocalY value (essentially its height, as the player sees it) to the controller's Z value (I've been casting it to int to make it easier to understand/ use). If the controller's Z is >= a platform's, their collisions turn off and the controller can pass. (this way, the controller can fall off the sides, and the platform's collision turns back on.).

Here's where it breaks (code is in C#):

 private void SetMyZ ()
     {
         if (isGrounded == true)
         {
            // this will probably be a Physics2D.OverlapPoint check
            // it checks the ground and sets myZ to the ground's Z if I am on the ground
         }
         else
         {
             Debug.Log("I am copying my newZ from my Sprite's localY.");
             newZ = playerSprite.tellMyHeight;
         }
         if (newZ != (int)myTransform.position.z)
         {
             Debug.Log(" I am now setting my Z");
             myTransform.position.Set(myBody.position.x, myBody.position.y, newZ);
         }
     }


When SetMyZ() is called in the FixedUpdate(), it slows the game down and then behaves wildly (the motion is choppy and the jump becomes very high). It's worth mentioning that the Sprite is on Update().

I think the problem is that using a Rigidbody2D and trying to set transform is slow, but RigidBody 2D won't handle Z and I need my collision to be 2 Dimensional.

Is the solution handling all control/movement directly with the Transform component? I need collision detection, so I'd rather use a rigidbody2D for that. I'd like to be able to use both methods to freely control my character via scripting, but Transform is looking too slow.

Thanks, I hope you can help me! I've got a pretty hard deadline coming up.

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

  • Sort: 
avatar image
0

Answer by TheAngerSpecialist · Oct 22, 2016 at 02:44 PM

I figured out a workaround:

{ myZVector.Set(0, 0, (int)newZ - myTransform.position.z); myTransform.position += myZVector; }

this works just fine. But it's a little annoying having to do all that math just to set one lousy point!

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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Echolocation in 2D top down game 0 Answers

Distribute terrain in zones 3 Answers

How to Position the object perfectly with the Screen Bounds 0 Answers

Multiple Cars not working 1 Answer

2D rigidbody top down movement problem 3 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