Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kamden · Dec 04, 2011 at 09:57 PM · transformtransform.position

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gregzo · Dec 04, 2011 at 10:14 PM 0
Share

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

avatar image kamden · Dec 04, 2011 at 10:33 PM 0
Share

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

avatar image gregzo · Dec 04, 2011 at 10:44 PM 0
Share

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...

avatar image kamden · Dec 04, 2011 at 10:49 PM 0
Share

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

avatar image gregzo · Dec 04, 2011 at 10:57 PM 0
Share

It's late here, off to bed, will give your funky children a thought tomorrow. Good night!

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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;
   }
}
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is it possible to intercept variable alteration/function calls on an object (From within the object)? 0 Answers

Getting the transform from a GameObject list 3 Answers

Confuse about transform.InverseTransformPoint 1 Answer

Possible way to improve performance moving large hierarchies? 1 Answer

My object is translating with speed but does not stop... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges