I solved it myself, solution has been left for others
Rigidbody character cannot move once parented to platform
I searched my buns off looking for a solution but couldn't find any sorry for asking :(
My problem is that I have a rigidbody character and when I land on a moving platform I make the platform a parent of the character and the character moves with the moving platform which is the desired effect. However the movement controls no longer work once I'm on the platform, my character is effectively glued to the platform indefinitely. I have trouble shooted the bajeebas out of this and have had no luck, many thanks in advance for any light you're able to shed on this.
I have attached both my platform and character scripts
Here is the characterController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour {
public AudioSource coinAudioSource;
public AudioSource goalAudioSource;
public AudioSource jumpAudioSource;
public float walkSpeed = 8f;
public float jumpSpeed = 7f;
public float turnSpeed = 2f;
public bool isController2D = true;
public bool pressedJump = false;
public bool isJumping = false;
public bool isGrounded = true;
public bool isHeading = false;
public HudManager hud;
Rigidbody rb;
Collider coll;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
coll = GetComponent<Collider> ();
hud.Refresh ();
}
// Update is called once per frame
void Update () {
//TurnHandler ();
if (isController2D) {
SideWalkHandler();
} else {
WalkHandler ();
}
JumpHandler ();
}
bool checkGrounded(){
//get player collision dimensions
float sizeX = coll.bounds.size.x;
float sizeY = coll.bounds.size.y;
float sizeZ = coll.bounds.size.z;
//find the bottom points of the player
Vector3 corner1 = transform.position + new Vector3(sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
Vector3 corner2 = transform.position + new Vector3 (-sizeX / 2, -sizeY / 2 + 0.01f, sizeZ / 2);
Vector3 corner3 = transform.position + new Vector3 (sizeX / 2, -sizeY / 2 + 0.01f, -sizeZ / 2);
Vector3 corner4 = transform.position + new Vector3 (-sizeX / 2, -sizeY / 2 + 0.01f, -sizeZ / 2);
//cast rays from bottom four corners
bool grounded1 = Physics.Raycast(corner1,new Vector3(0,-1,0),0.02f);
bool grounded2 = Physics.Raycast(corner2,new Vector3(0,-1,0),0.02f);
bool grounded3 = Physics.Raycast(corner3,new Vector3(0,-1,0),0.02f);
bool grounded4 = Physics.Raycast(corner4,new Vector3(0,-1,0),0.02f);
//Debug.Log ("Grounded: " + grounded1 + grounded2 + grounded3 + grounded4);
return(grounded1||grounded2||grounded3||grounded4);
}
bool CheckHeadHit(){
bool headed1 = false;
float sizeX = coll.bounds.size.x;
float sizeY = coll.bounds.size.y;
Vector3 centerOfTop = transform.position + new Vector3 (0f, sizeY / 2 + 0.01f, 0f);
Vector3 frontOfTop = transform.position + new Vector3 (sizeX / 2, 0f, 0f);
Vector3 rearOfTop = transform.position + new Vector3 (-sizeX / 2, 0f, 0f);
RaycastHit hit;
if (Physics.Raycast (centerOfTop, new Vector3 (0, 1, 0), out hit, 0.02f)) {
if (hit.collider.tag == "brick") {
hit.collider.SendMessage ("Hit");
headed1 = true;
}
}
return(headed1);
}
void JumpHandler(){
//get jump axis
float jAxis = Input.GetAxis ("ButtonA");
//Debug.Log ("jAxis: "+jAxis);
isGrounded = checkGrounded ();
//if jAxis exceeds 0f then make the player jump
if (jAxis > 0f) {
if(isGrounded && !pressedJump){
pressedJump = true;
//play jump sound
jumpAudioSource.Play();
//Jumping Vector
Vector3 jumpVector = new Vector3 (0f, jumpSpeed, 0f);
//Add jumping velocity
rb.velocity = rb.velocity + jumpVector;
}
} else {
pressedJump = false;
if (isGrounded) {
isJumping = false;
} else {
isJumping = true;
isHeading = CheckHeadHit ();
}
}
}
void WalkHandler(){
//set x and z velocities to 0
rb.velocity = new Vector3 (0f, rb.velocity.y, 0f);
//get distance
float distance = walkSpeed * Time.deltaTime;
Vector3 movement = new Vector3 (0f,0f,0f);
//get inputs
float hAxis = Input.GetAxis ("Horizontal");
//get vertical axis input
float vAxis = Input.GetAxis ("Vertical");
//get movement vector 3D
movement = new Vector3 (hAxis * distance, 0f, vAxis * distance);
//get current position
Vector3 currPosition = transform.position;
//get new position to move to
Vector3 newPosition = currPosition + movement;
//move to new position
rb.MovePosition (newPosition);
}
void SideWalkHandler(){
//set x and z velocities to 0
rb.velocity = new Vector3 (0f, rb.velocity.y, 0f);
//get distance
float distance = walkSpeed * Time.deltaTime;
Vector3 movement = new Vector3 (0f,0f,0f);
//get inputs
float hAxis = Input.GetAxis ("Horizontal");
//get vertical axis input
float vAxis = Input.GetAxis ("Vertical");
//get movement vector 2D
movement = new Vector3 (hAxis * distance, 0f, 0f);
//get current position
Vector3 currPosition = transform.position;
//get new position to move to
Vector3 newPosition = currPosition + movement;
//move to new position
rb.MovePosition (newPosition);
}
void OnTriggerEnter(Collider collider){
if (collider.tag == "coin") {
print ("Grabbing coin...");
coinAudioSource.Play ();
GameManager.gminstance.IncreaseScore (1);
print ("Score: " + GameManager.gminstance.score);
hud.Refresh ();
Destroy (collider.gameObject);
}
if (collider.tag == "goal") {
print ("Reached goal!");
goalAudioSource.Play ();
Destroy (collider.gameObject);
if (GameManager.gminstance.currentLevel == GameManager.gminstance.highestLevel) {
SceneManager.LoadScene ("you_finished");
} else {
GameManager.gminstance.GoToNextLevel ();
}
}
if (collider.tag == "enemy") {
print ("Enemy hit, Game Over!");
SceneManager.LoadScene ("game_over");
}
}
}
Here is my platformController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformController : MonoBehaviour {
Vector3 startPosition = new Vector3(0,0,0);
public float moveDistance = 1f;
public float moveSpeed = 1f;
public float moveDirection = 1f;
// Use this for initialization
void Start () {
startPosition = transform.position;
}
// Update is called once per frame
void Update () {
MoveX ();
}
void MoveX(){
//how much are we moving?
float movementX = moveDirection * moveSpeed * Time.deltaTime;
//check the new position
float newX = transform.position.x + movementX;
//f the new position is too far change direction
if (Mathf.Abs (newX - startPosition.x) > moveDistance) {
moveDirection *= -1f;
} else {
//if we can move, move
transform.Translate (new Vector3(movementX,0f,0f));
}
}
void OnTriggerEnter(Collider coll){
if(coll.tag == "player"){
coll.transform.parent = gameObject.transform;
}
}
void OnTriggerExit(Collider coll){
coll.transform.parent = null;
}
}
Answer by fghfghfg · Apr 26, 2018 at 02:15 PM
Sorry guys I solved it, here is the solution for anyone that has the same issue.. What I did was change my movement method from velocity based to translate based.
Follow this Question
Related Questions
GetAxis being missed in FixedUpdate work around? 1 Answer
Smooth motion rigidbody 1 Answer
Unity Rigidbody fps controller jumps higher each time. 0 Answers
Objects not acting according to physics 1 Answer
Moving RigidBody Smoothly 0 Answers