I am trying to make a player move with the moving rock, the player jumps on it and the rock takes her from a to b but right now the player is just sliding off the rock.
this is my code for the rock:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerattach : MonoBehaviour
{
public GameObject Player;
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "movingrock")
{
Player.transform.parent = other.gameObject.transform;
}
}
void OnTriggerExit(Collider other)
{
Player.transform.parent = null;
}
}
You can either parent the player temporarily to the object they are on, or drive the player’s relative position directly from the rock, which gives you explicit control.
When the player jumps off or falls off you would have to deparent and/or disable the position driving obviously.
Can you give more info like how is the Player in the inspector looking and where does this script go? If it is on the rock then maybe the error is that you check:
other.gameObject.tag == “movingrock”
But if the script is on the rock, then the other.gameObject is the Player and not the rock. So change it to:
if (other.gameObject == Player)…
Depending on how your Player is setup, you may also want to make the rigidbody kinematic:
Player.GetComponent().isKinematic=true;
Can you make a screenshot of your scene with the Player and the rock and the trigger of the rock. Best select the rock and include the inspector in the screenshot.
The script above has also a mistake I think because you set the parent of the rock to the player and not the other way around Try:
Ok, you don’t have a trigger on the rock! So OnTriggerEnter will not be called. If you have one of those errory, try to use a debugger or easier use Debug.Log(“I am now in OnTriggerEnter”); And if this does not appear in your console than you know this function is never called
So, add a simple BoxCollider to the rock and check “Is Trigger”. Edit it so that it is slightly above the rock above the mesh colider.
If this still does not work it may be because of the Third Person Character scripts. But try adding the trigger first.
Hm my guess would be that the player controller somehow uses the rigidbody to move the character. Was it an asset or is it the Unity controller? So maybe don’t set kinematic to true and if this gives you trouble (most likely it will) then have a look into using a FixedJoint instead of parenting like Serinx mentioned above. I’m off to bed for today, good luck!