I’ve got theory covered - reparent player object dynamically to object it’s on, so he’ll stick to moving platform, but for some reason it doesn’t work.
Both platform and player are physics-based rigidbodies. I’m not sure if problem lies in parenting or moving platform itself (debugging showed that it parents to correct object).
Player object just doesn’t stick to moving platform.
Here’s my platform code:
using UnityEngine;
using System.Collections;
public class SimpleMovingPlatform : MonoBehaviour {
public Vector3 velocity;
// Use this for initialization
void Start () {
}
void OnCollisionEnter(Collision other) {
velocity.x = -velocity.x;
velocity.y = -velocity.y;
velocity.z = -velocity.z;
}
// Update is called once per frame
void Update () {
this.rigidbody.velocity = velocity;
}
}
and this is character control script:
using UnityEngine;
using System.Collections;
public class PhysicBasedCharacterController : MonoBehaviour {
private bool isGrounded;
public float movementForce = 30;
public float jumpForce=20;
public float normalLimit =0;
public bool parentToColliders = true;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetAxisRaw("Vertical")>0){
this.rigidbody.AddForce(this.transform.forward * movementForce);
} else if (Input.GetAxisRaw("Vertical")<0){
this.rigidbody.AddForce(-this.transform.forward * movementForce);
}
if (Input.GetAxisRaw("Horizontal")>0){
this.rigidbody.AddForce(this.transform.right * movementForce);
} else if (Input.GetAxisRaw("Horizontal")<0){
this.rigidbody.AddForce(-this.transform.right * movementForce);
}
if ((isGrounded)&& (Input.GetButtonDown("Jump"))) {
this.rigidbody.AddForce(this.transform.up * jumpForce);
}
}
void OnGUI(){
//GUI.Box(new Rect(400,0,200,50),"isGrounded = "+isGrounded.ToString()+"
"+"Jump = "+Input.GetButton(“Jump”).ToString());
}
void OnCollisionStay(Collision other) {
foreach(ContactPoint temp in other.contacts) {
if((temp.normal.y>normalLimit)) {
isGrounded=true;
}
}
if ((parentToColliders) && (isGrounded)){ //check for isGrounded, so player won't get parented to insane things like walls or ceiling and thus won't glitch out of level.
this.transform.parent = other.gameObject.transform;
}
}
void OnCollisionExit(Collision other) {
isGrounded=false;
}
}
Can you help me?
If it does matter, character is using Capsule collider and platform uses box collider.