Search Unity

Player not moving when childed to Prefab platforms

Discussion in 'Scripting' started by tedthebug, Jul 3, 2015.

  1. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Hi, I know the whole issue of having players on platforms move with them & not change their scaling is a common problem but I have that partially worked out. What I'm having issues with is when the player moves onto a platform that was instantiated in game.
    I have a prefab platform (log) that is scaled & attached to an empty game object that has scale of (1,1,1) so the player isn't rescaled when childed to it. I child the player to the empty game object.
    If I drag a prefabbed log into the game the player can move onto it (it gets childed to the empty game object), moves with the log, & is able to move along the log, off the log (is orphaned again) & back onto the log again.
    When I have the exact same prefab spawned during the game the player gets childed to the empty game object as soon as it comes in contact, gets dragged along with it, but is unable to move by using the controls at all, I can't even move off of the log again.

    I'm using a rigidbody player with my own movement script & the bit of code I'm using to parent is:
    .
    void OnCollisionStay(Collision other){

    // stick player to log when on it

    if (other.gameObject.tag == "Log") {

    transform.parent = other.gameObject.transform.root;
    }



    Any ideas of why the prefabs spawned in game are causing the issue? Any help is greatly appreciated
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    [code ][/code ] tags when you paste code into the forums, it really helps with readability, there is a sticky about those at the top of the scripting section.


    How are you doing the movement of the player?
     
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Ok, sorry I missed that bit about code.
    This is the movement bit. I've only been learning code a few months so this is likely not the best way to do it but it was doing what I wanted so I stuck with it. I'm not using add force or anything as I want the character to stop as soon as the player stops pressing a key & I want instant max speed when they do press a key.:

    Code (CSharp):
    1. // get direction
    2.             targetVelocity = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    3.             // prevent diagonal moves
    4.             if (Input.GetAxisRaw ("Horizontal") != 0 && Input.GetAxisRaw ("Vertical") != 0)
    5.                 targetVelocity = Vector3.zero;
    6.  
    7.             // move the player
    8.             targetVelocity = transform.TransformDirection (targetVelocity);
    9.             rigidbody.position += targetVelocity * Time.deltaTime * speed;  
    10.  
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the if on line 4 looks wrong, shouldn't that be "set velocity to 0 if there is no input" not "set velocity to 0 if there is both h and v input"?
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I don't want to let them move diagonally so I just stop them moving if they hold down 2 keys
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I gave up as I couldn't work it out why it was inconsistant with pre-placed prefabs as opposed to ones that were instantiated in-game. I switched out the rigidbody & am using the character controller after re-doing all the collision detection & movement script (it was just easier in the end).