简体   繁体   中英

Why does my object move too far in Unity?

I am really new to Unity so I have some problems...

I want to create a 2D game. I made a button. when pressed the player schould go to x:0, y:70. So I created a canvas. in this canvas a Button: Button In the same canvas I created the player: Player

I made this code:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

   public void moveUp()
   {
       transform.position = new Vector3(0, 1 ,0);
   }
}

but the player doest go to 0,1. It goes to 0,60...

How do I fix this?

transform.position this is the global position. If you are checking the position shown in the inspector, that is the localPosition , which is relative to the parent, and is affected by the transform of all of its ancestors in the hierarchy.

If you want to your transformation changes to be the same as what the inspector shows you need to modify transform.localPosition .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM