- Home /
geting GameObject transform position gives Spasmic numbers
Hello everyone, here's the code to my problem. I'll put the description of the problem under it.
GameObject player;
public Vector2 playerPos;
public Vector2 shopPos;
public bool inRange = false;
public Vector2 distance;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update () {
playerPos = new Vector2(player.transform.position.x, player.transform.position.y);
shopPos = new Vector2(transform.position.x, transform.position.y);
distance = playerPos - shopPos;
if(distance.x <= 3 && distance.x >= -3 &&
distance.y <= 3 && distance.y >= -3){
inRange = true;
}
else{
inRange = false;
}
OnGUI();
}
Okay so when i run this script the Y-Axis numbers are going crazy changing from 5 to -41 in a split second and then switching back to 5 and then down to -12, and keeps going like that. Since the GameObject with the Tag "player" consists of many children(and the script works fine with just an ordernary Cube), I thought if I'd fill the GameObject "player" with the smallest child the spasms would stop but it didn't.
I am completely Clueless. Please healp me, Kamden.
Hmmm, crazy transform.position numbers, maybe you have overlapping colliders which aren't set to isTrigger? If you trace playerPos.y, is that where the funk happens? Or is it in distance.y? And is the player object moving when you get crazy ys, or does it go haywire even when the player object is static?
In other words, knowing which y is the problem and in which circumstances would probably help you solve this on your own... Will still gladly help if my limited knowledge is enough, though!
Gregzo
Hi gregzo, thx for the quick reply. So it's not an "overlapping collider" problem since it only has one collider. As for where "the funk happens" it's at the playerPos.y and it happens whether it moves or not. But it's only according to the script the Y is odd, when I check the GameObject with the tag "player" the Y position is not junking around at all.
//$$anonymous$$amden
Are you using gravity? Rigidbodies? Fixed or hinge joints? Did you try tracing the y of an empty "player" GameObject? Just trying to narrow it down...
Yes I'm using gravity within a Rigidbody. I've tried to use an empty "player" GameObject aswell as a Cube with a Rigidbody and then the y does not freak out. Perhaps it funks with the children?, but why only the Y-axis then? :o
It's late here, off to bed, will give your funky children a thought tomorrow. Good night!
Answer by Lo0NuhtiK · Dec 25, 2011 at 04:52 AM
I came across your question and I'm pretty bored, so I just made a game object with 6 children, 7 grandchildren, and 9 great-grandchildren lol ... but I didn't have any crazy number problems. I changed your script a bit, though, since I moved it over to UnityScript, so I don't know if there might be something weird in yours or what's going on there. I tried this with & without child objects/gravity/rigidbodies , no crazy numbers happening for me... Anyway, here's what I used in UniScript if you want to try it and see if it works how you want it to ->
var player : Transform ;
var shop : Transform ;
var playerPos : Vector2 ;
var shopPos : Vector2 ;
var range : float = 3.0 ;
var distance : float ;
var inRange : boolean = false ;
function Start(){
player = GameObject.FindGameObjectWithTag("Player").transform ;
shop = transform ;
}
function Update(){
playerPos = Vector2(player.position.x , player.position.y) ;
shopPos = Vector2(shop.position.x , shop.position.y) ;
distance = Vector2.Distance(playerPos , shopPos) ;
if(distance <= range){
inRange = true ;
}
else{
inRange = false ;
}
Debug.Log(inRange) ;
}
Hmm.. Like I said, I'm pretty bored, so I'm going to go ahead and try to put that into C# for you even though I don't really know the language. I can translate it most of the time into UniScript when it's not very complex like this one isn't, but haven't tried putting it back yet :D Here goes ->
Transform player;
Transform shop;
public Vector2 playerPos;
public Vector2 shopPos;
public float range = 3.0f; //<--- ????
public float distance;
public bool inRange = false;
void Start(){
player = GameObject.FindGameObjectWithTag("Player").transform;
shop = transform;
}
void Update(){
playerPos = new Vector2(player.position.x, player.position.y);
shopPos = new Vector2(shop.position.x, shop.position.y);
distance = new Vector2.Distance(playerPos, shopPos);
if(distance <= range){
inRange = true;
}
else{
inRange = false;
}
}