- Home /
transform.position problem
Hi,
I have an empty game object with the following script attached:
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
public GameObject playerCharacter;
private GameObject _pc;
private Vector3 _playerSpawnPos;
void Start () {
_playerSpawnPos = new Vector3(20.0f, 4.0f, 20.0f);
//_pc = Instantiate( playerCharacter, _playerSpawnPos, Quaternion.identity ) as GameObject;
_pc = Instantiate( playerCharacter, Vector3.zero, Quaternion.identity ) as GameObject;
_pc.name = "wtf";
_pc.transform.position = _playerSpawnPos;
}
}
I have a prefab based on the Camera Relative Controls controller. This prefab becomes my "playerCharacter" Game Object. When I transform the position the joysticks show up at (-20, -4, 0). So they aren't showing up in the camera. The capsule and the camera transform ok, just not the joysticks. What am I missing?
Thanks for any help!!
Cheers!
Jim
Don't see anything about joysticks on your code? I assume this is another game object? Is it a child object in the prefab? Any child objects are going to be repositioned relative to the parent when you transform.
Hi Dave,
The joysticks are part of the Standard Assets ($$anonymous$$obile) Camera Relative Control prefab. I place that prefab into the Public GameObject playerCharacter. I'm just utilizing the prefab to get some control elements in my view, but as I mention they aren't showing up.
Jim
I'm not sure I follow that, but you don't want to put the joysticks component in another object that itself might move around.
This control is devised to use the joysticks for character movement on a mobile device. Admittedly I could be misunderstanding how Unity meant it to be used and perhaps I should be moving a child object rather than the parent. The prefab has the following hierarchy:
Camera Relative Controls - Camera Relative Controls - - Camera Pivot - - - Camera Offset - - - - Camera - - Player - - - ReplaceWithRealCharacter - Left Joystick - Right Joystick
Perhaps I should be applying the movement to the second tier Camera Relative Controls?
Well that sucked ... Try a different approach ... The hierarchy of the prefab is:
Camera Relative Controls
- Camera Relative Controls
- - Camera Pivot
- - - Camera Offset
- - - - Camera
- - Player
- - - ReplaceWithRealCharacter
- Left Joystick
- Right Joystick
Answer by jvcraft87 · Feb 12, 2013 at 10:33 PM
DaveA,
You were correct in that I shouldn't be moving the joysticks. Thanks for that pointer!
So the corrected code is:
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
public GameObject playerCharacter;
private GameObject _pc;
private Vector3 _playerSpawnPos;
void Start () {
_playerSpawnPos = new Vector3(20.0f, 4.0f, 20.0f);
_pc = Instantiate( playerCharacter, Vector3.zero, Quaternion.identity ) as GameObject;
_pc.name = "wtf";
_pc.transform.Find("Camera Relative Controls").position = _playerSpawnPos;
}
}
So I basically find the child that holds the player character and the camera (this child is at the same level as the Joysticks), and transform the position of that child. Voila!
Again, thanks for the pointer DaveA!
Your answer
![](https://koobas.hobune.stream/wayback/20220613092253im_/https://answers.unity.com/themes/thub/images/avi.jpg)