- Home /
Different positions in just one scene. 10 x axys != 4 x axys
My first level scene was working fine, but I had to work in the complete game design, so I add more bricks and put my "spike" elsewhere. As the position was changed, my spike wasn't doing what it was supposed to do, obviously. I just needed to set other values and it could work like the spikes from sonic the hedgehog, coming out from a wall and then going back. The positions are not right though. Here is the code:
using UnityEngine;
using System.Collections;
public class Espinho : MonoBehaviour {
public Vector3 movimentacao;
private PausarGame pausarGame;
public float XMaximo;
public float XMinimo;
void Start()
{
pausarGame = FindObjectOfType (typeof(PausarGame)) as PausarGame;
}
void Update () {
if (!pausarGame.pause) {
transform.position += movimentacao;
}
VaiVolta ();
}
void VaiVolta()
{
if (transform.position.x >XMaximo) {
movimentacao*=-1;
}
else if (transform.position.x <XMinimo) {
movimentacao*=-1;
}
}
}
When it is located at -1.651396 x axys and for example I want it to go to 6 x axys, it goes to 0.7 and then change directions. It is the same when it goes back, when I put -1, it goes to -6.2 and then goes back again. It is working fine in other scenes, but in my first level it is kinda changing the values and I don't know what is going on. It is working as it should, but not working with the values I really want it to work. I thought it was because of the prefab or something, but it is happening the same when I just add a new object with the same script and with the fireball, which has almost the same script and has to do the same the spike is supposed to do. Thanks in advance.
If your spikes were a child of an empty gameObject, you could use transform.localPosition . With your current method, you would need to store the starting position of the spikes, then use that in your calculations :
Vector3 startPos;
// in Start
startPos = transform.position;
// in method
if (transform.position.x > startPos.x + X$$anonymous$$aximo) {
I'll have to be putting different values anyways. I wanna know why the values are different than they should really be. I put 3 and it goes to 7, but why?