Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

How to Allow a character to still jump once parented to a moving platform

$
0
0
Good day, In my scene, I have a moving platform that moves up and down (Y-Axis). In my script when my player collides with this platform, then the platform is parented to the player's rigidbody so they move together. The problem is I am unable to jump off this platform - as they share the same y-velocity (I assume). public class PlayerMovement : MonoBehaviour { public float speed = 6f; public float lookSpeed = 10f; Vector3 movement; Animator anim; Rigidbody playerRigidbody; bool grounded = false; float maxSlope = 60; void Awake (){ anim = GetComponent (); playerRigidbody = GetComponent (); } void FixedUpdate (){ float h = Input.GetAxisRaw ("Horizontal"); float v = Input.GetAxisRaw ("Vertical"); bool jumping = false; if (Input.GetButtonDown("Jump")) { jumping = true; } Move (h, v, jumping); Turning (h,v); Animating (h, v); playerRigidbody.AddForce(Physics.gravity * playerRigidbody.mass); } void Move (float h, float v, bool jumping){ if (jumping && grounded) { playerRigidbody.velocity += new Vector3(0,12,0); anim.Play("Jump"); } else movement.Set (h, 0f, v); movement = movement.normalized * speed * Time.deltaTime; playerRigidbody.MovePosition (transform.position + movement); } void Turning (float h, float v){ if(h != 0 || v != 0) transform.rotation = Quaternion.LookRotation(movement * lookSpeed); } void Animating (float h, float v){ bool walking = h != 0f || v != 0f; anim.SetBool ("isRunning", walking); } void OnCollisionStay(Collision collider) { foreach (ContactPoint contact in collider.contacts) { if(Vector3.Angle(contact.normal, Vector3.up)< maxSlope) grounded = true; } Rigidbody otherRigidbody = collider.rigidbody; if (otherRigidbody != null && otherRigidbody.gameObject.tag == "MovingPlatform") { playerRigidbody.transform.parent = collider.transform; } } void OnCollisionExit(Collision collider){ grounded = false; Rigidbody otherRigidbody = collider.rigidbody; if (otherRigidbody != null && otherRigidbody.gameObject.tag == "MovingPlatform"){ playerRigidbody.transform.parent = null; } } } I am able to walk off the platform just not jump off. I suspect I will encounter the same problem with a platform moving on the horizontal axis? I have tried other ways to get the player to move with the platform such as adding a physicMaterial and making their velocities equal each other but parenting seemed to be the only one to work. Thank you for helping.

Viewing all articles
Browse latest Browse all 302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>