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 /
avatar image
0
Question by asemiter19 · Jul 21, 2018 at 07:50 AM · 2dmoving platform

problem with moving platform

i have problem with circle moving platform
i have Circle, the circle rotate around itself and move right and left
when player jump on the circle, the player be child of the Circle
it works fine, but sometime doesn’t (not be a child of a circle)

Circle attached with PlatformController
Player attached with PlayerController

PlatformController.cs

  public class PlatformController : MonoBehaviour {
   public int rotateSpeed;
   public int moveSpeed;
   public bool isMoving;
   void Start () {
   rotateSpeed = Random.Range (1,7);
   moveSpeed = Random.Range (1,7);
   }
   void FixedUpdate () {
   transform.Rotate (0,0,1);
   if (isMoving) {
   transform.Translate (moveSpeed * Time.deltaTime ,0,0 , Space.World);
   }
   }
   void OnTriggerEnter2D (Collider2D other) {
   if (other.gameObject.tag == "rightBorder") {
   moveSpeed = -moveSpeed;
   }
   if (other.gameObject.tag == "leftBorder") {
   moveSpeed = Mathf.Abs (moveSpeed);
   }
   }
  }

Player Controller Script

  public class PlayerController : MonoBehaviour {
    
  Rigidbody2D rb;
   public float jumpSpeed;
   public bool touchGround;
    
  // Use this for initialization
   void Start () {
   rb = GetComponent();
   }
   // Update is called once per frame
   void Update () {
   Jump ();
   }
   public void Jump () {
   if (Input.GetKeyDown (KeyCode.Space)) {
   if (touchGround) {
   rb.constraints = RigidbodyConstraints2D.None;
   rb.velocity = new Vector2 (0, jumpSpeed);
   }
   }
   }
   void OnCollisionEnter2D (Collision2D other) {
   if (other.gameObject.tag == "ground") {
   touchGround = true;
   this.transform.parent = other.gameObject.transform;
   rb.constraints = RigidbodyConstraints2D.FreezePositionY;
   }
   }
   void OnCollisionExit2D (Collision2D other) {
   if (other.gameObject.tag == "ground") {
   touchGround = false;
   this.transform.parent = null;
   rb.constraints = RigidbodyConstraints2D.None;
   }
   }
  }

i need to know why sometime the player in the game can’t be child of the circle ?
Thanks , sorry for my english

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 madks13 · Jul 23, 2018 at 10:39 AM

If you placed the jump inside the Update, keep in mind that you can have 200+ updates per second, depending on the scene complexity, your computer's power and some other factors.

You try to detect a keyboard key being pressed, but at the rate of update, if i recall correctly, it will detect it several times before the key is let up, and setting/unsetting the parent at that rate might seems it look like sometimes it is not working, but you just stopped on the part when it was unset.

It's enough for Unity to catch the jump key 2 times in a row and you will unset the parent before the player character left the ground. Velocity doesn't teleport the player object outside the reach of ground, but accelerates it in a direction, which might not be instant (depends on the colliders position and size and how you detect ground). In any case, add a few Debug.Log inside methods to see if they don't activate too much.

Other than that, i don't see anything else as possible problem, based on your description.

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

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Player has jittery and slow movement when child of an animated object. 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

An easy way to get the global position from a child object 5 Answers

Adding kinematic rigidbody makes OnTriggerExit break 0 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