- Home /
How can I parent the player to another GameObject (Vehicle)?
Hi, I'm working on a game, where one of the mechanics, is that you can ride on the back of a truck.
And yes, I'm aware, I'm sure this question has been asked a million times before, but I feel like I've looked at / tried everything, and I cannot seem to be able to get / find / get anything that works.
In order to achieve this, I imagined making the player be parented to the truck, and the truck would be able to move around, moving the player around with it. I looked it up, and found a script, and tried it. It would not work. Instead, it would warp the character / player, flattening the character, and widening the character. The character would somewhat move, but would mainly get stretched and distorted, and if I drove off the platform, the character would get stretched along the y / height axis, up into the air / sky, then skew (or maybe rotate, I can't tell, entirely, sorry,), towards the vehicle.
Here is my current script: using UnityEngine; using System.Collections;
public class RideTruck : MonoBehaviour
{
bool guiShow = false;
bool isRiding = false;
public GameObject handle;
public GameObject player;
public GameObject truck;
public GameObject truckparent;
public int rayLength = 2;
void Update()
{
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, out hit, rayLength))
{
guiShow = true;
if (Input.GetKeyDown("e") && isRiding == false)
{
isRiding = true;
guiShow = false;
truckparent.transform.parent = truck.transform;
player.transform.parent = truckparent.transform;
}
else if (Input.GetKeyDown("e") && isRiding == true)
{
isRiding = false;
guiShow = false;
transform.parent = null;
}
}
else
{
guiShow = false;
}
}
void OnGUI()
{
if (guiShow == true && isRiding == false)
{
GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Ride Truck");
}
else if (guiShow == true && isRiding == true)
{
GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Stop Riding");
}
}
}
Also, the character will either distort, or not move at all. As of right now, what it seems to be doing, is no matter what, it won't move. It sometimes will distort, but it never seems to move anymore now.
Answer by NoahDaNerd · Nov 22, 2020 at 03:37 PM
transform.parent = gameObjectYouWantAsParent.transform;
or
transform.parent = transformYouWantAsParent;
on player script
If you do not want the player to squish, then the parent needs to be of scale 1, 1, 1. If the parent is not of scale 1, 1, 1, then you need to use a custom parenting system to apply any translations and rotations that the parent has to the player. There are many of these online.
@NoahDaNerd Yeah, the truck has non-uniform scaling, but the player does not. And, sorry, but do you know of any of these custom parenting systems? I can't find any. I think I might have something like that, but the character won't move, no matter what, it seems.
Answer by S-khan · Nov 22, 2020 at 03:51 PM
Here is your solution with a proper explanation. Always try to maintain (1,1,1) scale.
@S-khan I'm pretty sure I've kind of tried this already, I have an empty GameObject with a scale of 1,1,1, set as a child to the truck. I have another empty GameObject, with a scale set to 1,1,1, that I've made the parent of the First Person Controller. I've tried using a whole bunch of combinations of these, and none seem to work. (Like, I've tried parenting the Empty Player GameObject to the Empty Truck GameObject, Player to Empty Truck, etc..). It will either won't move, or distort and stretch like crazy. I have no idea what's going wrong.
Then, try Transform.SetParent(), with worldPositionStays is equal to true.
Ok, so I've essentially got something that works, here's the code:
using UnityEngine;
using System.Collections;
public class RideTruck : $$anonymous$$onoBehaviour
{
bool guiShow = false;
bool isRiding = false;
public GameObject handle;
public GameObject player;
public GameObject truck;
public GameObject playerAvatar;
public FirstPersonAIO aio;
//public GameObject truckparent;
public int rayLength = 2;
void Start()
{
playerAvatar = GameObject.FindGameObjectWithTag("PlayerChild");
GameObject fpsaio = GameObject.Find("FirstPerson-AIO");
aio = fpsaio.GetComponent<FirstPersonAIO>();
}
void Update()
{
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, out hit, rayLength))
{
guiShow = true;
if (Input.GetKeyDown("e") && isRiding == false)
{
isRiding = true;
guiShow = false;
//truckparent.transform.parent = truck.transform;
//player.transform.parent = truckparent.transform;
player.transform.parent = truck.transform;
playerAvatar.transform.parent = truck.transform;
aio.playerCan$$anonymous$$ove = false;
Debug.Log("Player$$anonymous$$ovement Should be Disabled!");
truck.GetComponent<SimpleCarController>().enabled = true;
}
else if (Input.GetKeyDown("e") && isRiding == true)
{
isRiding = false;
guiShow = false;
transform.parent = null;
player.transform.parent = null;
playerAvatar.transform.parent = player.transform;
aio.playerCan$$anonymous$$ove = true;
Debug.Log("Player$$anonymous$$ovement Should Be Enabled!");
truck.GetComponent<SimpleCarController>().enabled = false;
}
}
else
{
guiShow = false;
}
}
void OnGUI()
{
if (guiShow == true && isRiding == false)
{
GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Ride Truck");
}
else if (guiShow == true && isRiding == true)
{
GUI.Box(new Rect(Screen.width / 2, Screen.height / 2, 100, 25), "Stop Riding");
}
}
}
However, the one issue is, is that the FirstPerson-AIO (The $$anonymous$$ain, Player GameObject,), won't move with the parent. The Avatar will, and, the FirstPerson-AIO GameObject is currently parent under the truck, but it just won't move with it. I need it to move with it, since it has the player movement script, and is what I'm using to pop up the interaction for the user to Start / Stop Riding.
@S-khan Also, I guess I should probably try to explain what I'm really trying to do a bit more, because I realize now that I feel like even if I get this to work in the end, in the end, I feel like it isn't exactly like how I truly need it to be, which would probably (I'm guessing, at least,), change things. So, anyways, I'm essentially going to want to try to have the player grab onto this handle (once they look within the collision box, currently highlighted, and of which is a box with an orange transparent, shadeless material,), and then the trash truck will be able to move around, and they will still hold on, until the trash truck stops at it's next stop / destination / stop, in which the player will be able to press E again to get off.