- Home /
transform.position is instantiating at the correct place but not staying there
Hey everyone,
I have created a specific starting position for my Player but on start it moves towards 0, 0 , 0. I'm not sure why when the rest of the time on touch it moves to exactly the correct places. Only on start does it "float" away to a different location. Below is my player code for start and movement script. I'm not sure if it has something to do with the Euler. I don't think it has anything to do with my rotation condition statement as I've removed that and tested it with and without it. My Movement method is called in the Update method (which is not posted) not the start method. Thanks in advance! (PS, ignore the call to the uiManager, it's being used in many other places in the Player script)
void Start () {
transform.position = new Vector3(.31f, 2.82f, 0);
_animator = GetComponent<Animator>();
_uIManager = GameObject.Find("Canvas").GetComponent<UIManager>();
if (_uIManager != null)
{
_uIManager.UpdateLives(lives);
}
}
private void Movement() {
if (NinjaTimeOn == true)
{
Time.timeScale = .5f;
}
else
{
Time.timeScale = 1;
}
//overall movement method (time.timescale to maintain players speed throughout game)
transform.position = Vector3.Lerp(transform.position, _myPosition, _speed * Time.deltaTime * 1 / Time.timeScale);
//move player to point touched 1 time
if (Input.touchCount > 0)
{
Touch _touch = Input.GetTouch(0);
Vector3 _touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(_touch.position.x, _touch.position.y, 10));
_myPosition = _touchPosition;
_animator.SetTrigger("Player_Run");
//if _tranform.position is between bounds of x,x and y, y TR, TC, TL, BR, BC, BL
if (_myPosition.x >= 3.76f && _myPosition.x <= 8.8f && _myPosition.y >= 0.0f && _myPosition.y <= 4.7f)
{
_myPosition = new Vector3(6.2f, 2.75f, 0);
}
else if (_myPosition.x >= -3.02f && _myPosition.x <= 3.75f && _myPosition.y >= 0.0f && _myPosition.y <= 4.7f)
{
_myPosition = new Vector3(.31f, 2.82f, 0);
}
else if (_myPosition.x >= -8.31f && _myPosition.x <= -3.03f && _myPosition.y >= 0.0f && _myPosition.y <= 4.7f)
{
_myPosition = new Vector3(-5.48f, 3.04f, 0);
}
else if (_myPosition.x >= 3.76f && _myPosition.x <= 8.8f && _myPosition.y >= -4.94f && _myPosition.y <= -.1f)
{
_myPosition = new Vector3(6.54f, -3.48f, 0);
}
else if (_myPosition.x >= -3.02f && _myPosition.x <= 3.75f && _myPosition.y >= -4.94f && _myPosition.y <= -.1f)
{
_myPosition = new Vector3(-.2f, -3.37f, 0);
}
else if (_myPosition.x >= -8.31f && _myPosition.x <= -3.03f && _myPosition.y >= -4.94f && _myPosition.y <= -.1f)
{
_myPosition = new Vector3(-7, -3.37f, 0);
}
}
//if player on y is > 2.45 rotate to face down
//if player on y is < -2.45 rotate to face up
if (transform.position.y > 2.0f)
{
transform.localRotation = Quaternion.Euler(180, 0, 0);
}
else if (transform.position.y < -2.0f)
{
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
}
Answer by michi_b · Nov 06, 2018 at 06:24 AM
Your movement code always pulls transform.position towards "_myPosition". In the posted code, this is only assigned on touch. Maybe you forgot to initialize _myPosition to the same value as transform.position on Start()? If it's not assigned, it will have its default value (0|0|0), which would explain the observed behavior.
so would i just write
_myPostion = transform.position or would i need to write out the whole new vector3()