- Home /
Cannot move a Gameobject to desired location
Hey guys , I want to move my player to an empty GameObject (stealthKillPoint) so that I can then kill a guard. The stealthKillPoint is a child of the Guard gameobject and the below script is attached to the stealthKillPoint gameobject.
void MoveThePlayer()
{
player.transform.position=new Vector3(stealthKillPoint.position.x,player.transform.position.y,stealthKillPoint.position.z);
player.transform.LookAt(transform.parent.transform.position); //this is to look at the guard
KillingAnimation();
}
I think that the player should move to the stealthKillPoint but it stays in the same position when MoveThePlayer function is called. I printed the value of player's position which is the same as that of stealthKillPoint (-18.2,0.5,17.4) but the inspector shows that player's position is (-16,0.5,18.2). When I change the player's position manually in the inspector the player moves to the position where I want it to but I cannot make the script do the same. Player is not a child of any other gameobject and I tried using localPosition but the player again stays in the same position just its rotation is changed(I don't know why its rotation is getting affected by localPosition).I would be glad if any one could help me.
Thanks in advance.
(P.S. Nothing is static in the script)
can you show how game objects are set up in inspector?
StealthKill Distance is the child of Guard whom I am trying to kill. The player is not a child of any other GameObject. I tried to change the position of the player in the above script to(0,0,0) but it still stays at the same position. In the console it still prints its position as (0,0,0) but the inspector says otherwise. I am really confused here.
Answer by DenisIsDenis · Jun 20, 2021 at 09:41 AM
If the player object has a Character Controller
, then it should be disabled before moving it in the script:
void MoveThePlayer()
{
GetComponent<CharacterController>().enabled = false;
player.transform.position =
new Vector3(stealthKillPoint.position.x,player.transform.position.y, stealthKillPoint.position.z);
player.transform.LookAt(transform.parent.transform.position); //this is to look at the guard
GetComponent<CharacterController>().enabled = true;
KillingAnimation();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613035215im_/https://answers.unity.com/themes/thub/images/avi.jpg)