Below is my code. I was trying to make a generic tool script for my game, but before I could even get to setting its position, I noticed it squished and stretched in strange ways relative to its parent, based on scale and rotation. I can’t find a way to disable that. Also I’m a bit of a beginner, if that explains this. Anyway you may see remnants of me trying to reset its position after parenting it, which of course didn’t work. I also tried to use an empty gameobject scaled at 1,1,1 as the “hands” to see if that’d fix it, but alas, no. Nothing goes my way in this engine, I swear…
using UnityEngine;
public class toolscript : MonoBehaviour
{
public bool pickedup = false;
public GameObject hands;
public GameObject tool;
public Rigidbody toolRB; // comment out for no physics on the tool
private Vector3 originalScale;
void Start()
{
originalScale = tool.transform.lossyScale;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject == tool)
{
pickedup = true;
}
}
}
if (pickedup)
{
tool.transform.SetParent(hands.transform, true);
toolRB.isKinematic = true; // comment out for no physics on the tool
GetComponent<Collider>().enabled = false;
if (Input.GetMouseButtonDown(1)) {
pickedup = false;
}
}
else
{
tool.transform.parent = null;
toolRB.isKinematic = false; // comment out for no physics on the tool
GetComponent<Collider>().enabled = true;
}
}
}