0
\$\begingroup\$

I have a prefab named Spear.

It contains a script like this:

using System.Collections.Generic;
using UnityEngine;

public class PickUp : MonoBehaviour
{
    public GameObject Hand;
    public float WeaponDamage;
    public Enemy enemyScr;
    public bool IsInHand;

    public void Start()
    {
        enemyScr = GameObject.Find("EvilTriangle").GetComponent<Enemy>();

        Hand = GameObject.Find("Hand");
    }

    public void OnMouseDown()
    {
        this.transform.position = Hand.transform.position;
        this.transform.parent = GameObject.Find("Hand").transform;
        transform.eulerAngles = new Vector3(0, 0, 0);
        GetComponent<Rigidbody2D>().isKinematic = true;
        IsInHand = true;
    }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q) && IsInHand == true)
        {
            this.transform.parent = null;
            GetComponent<Rigidbody2D>().isKinematic = false;
            IsInHand = false;
        }
    }
}

In play mode, when I click on the spear (and it is vertical in the scene), everything is looking fine.

But when I click on the spear when it is horizontal, (and as you can see in the script, I'm trying to rotate it to 0), its scale is getting weird.

Normal looking spear: enter image description here

Weird looking spear: enter image description here

This happens every time I drop the spear vertical (while playing), it gets thinner and thinner.

I have no idea what is causing this problem.

Any help will be appreciated!

\$\endgroup\$
8
  • \$\begingroup\$ I'd bet you have a non-uniform scale applied to the parent object you're placing that object into, or one of its grandparents. See previous Q&A like Object scales strangely when setting y rotation after parenting and Object scaling when parenting by script. Does the advice in these existing threads help solve your problem? \$\endgroup\$
    – DMGregory
    Commented Oct 12, 2022 at 16:07
  • \$\begingroup\$ I don't really know how can I change the non-uniform scale to get this working to be honest \$\endgroup\$
    – Makaron
    Commented Oct 12, 2022 at 17:05
  • \$\begingroup\$ Make a parent object that has no scale applied. Add a child object for the visual representation you need to change. Apply the scale to the child object, not the parent. Keep any non-uniform scales at the leaf nodes of your hierarchy, never in a parent. I explain this in one of the existing answers I've linked. \$\endgroup\$
    – DMGregory
    Commented Oct 12, 2022 at 17:11
  • \$\begingroup\$ How should I do it? When I set it to 0 the object gets scaled like this too. \$\endgroup\$
    – Makaron
    Commented Oct 12, 2022 at 17:24
  • \$\begingroup\$ What is the "it" you're setting to zero? I did not suggest setting anything to zero. \$\endgroup\$
    – DMGregory
    Commented Oct 12, 2022 at 18:09

0

You must log in to answer this question.

Browse other questions tagged .