Search code examples
unity-game-engineposition2d

UNITY 2D - Can't change Child's position


I have created a project and put a character in it, a Ninja. I drew every part of his body and put them together into one organized gameObject. It is organized as this. Ninja Organization

NOTE : The Left_Arm GameObject has a parent so it can be rotated by the shoulder like a real arm

Now here comes the problem. I've searched and searched for hours and haven't found a solution to something that is surely so simple and dumb. In a script, I try to move the Left_Arm parent for a little animation. Here's the script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NinjaShooting : MonoBehaviour {

    [Header("GameObjects")]
    public Transform leftArm;
    public Transform rightArm;

    [Header("Other")]
    public bool shooting = false;

    // Update is called once per frame
    void Update () {

            Debug.Log(leftArm.localPosition);
            leftArm.localPosition = new Vector3(100, 100, 100);
            Debug.Log(leftArm.localPosition);
            shooting = false;

    }  
}

The public variables are all assigned and everything, there are no errors in the console, but the line leftArm.localPosition = new Vector3(100, 100, 100); doesn't seem to do a thing to my character.

Here's is the Console after running it : Image

The console seems alright. The start position is (-0.3, 0.2, 0) and after leftArm.localPosition = new Vector3(100, 100, 100); the position is debugged as (100, 100, 100) as it should be. But the position changes in the inspector, nor in the game window...

Additionnally, the starter position is debugged every frame, but it shouldn't. So I assume that my position isn't taken into account or something.

I've done this so many times, and always has worked.

I even tried with a new project and recreated it, and there, mysteriously, it worked. So there is someting wrong with my organization or something. Sorry if this is a stupid question.

Thank you in advance

PS : Here is an image of the script in the editor, just in case there something wrong with that : Image

PS : The script is a it weird because I simplified it :). But this doesn't work too


Solution

  • This issue is likely caused by an Animator controller "locking" properties that are not actually part of its Motion. In this case, it is "locking" the Transform's position. Unity does not report any warnings or errors when you try to modify those properties, even if they are being controlled by an animation.

    The typical offender is the Write Defaults property being set to true for your Animation States in your animation controller. Have a look here at the documentation for what Write Defaults does, and it will make more sense to you. I suggest changing it to false for all animation states that do not need it.