- Home /
Problem how to access Y-position value
I am making a voice-controlled variant of Flappy Bird. To make the bird move across the Y-axis, I have a function that converts pitch note (frequency) to the corresponding Y-position. Just like the original game, the pipes have a randomly generated opening through which the bird has to pass. Every time the bird successfully passes through a pipe opening, I need to save it in my database in following data format:
-seqNum: (int) keeps track of the pipe order
-maxFreq: the opening's upper threshold value, converted from Y position to frequency
-minFreq: the opening's lower threshold value, converted from Y position to frequency
-hitFreq: the frequency that the user actually hit (= the bird's position)
My problem is not "converting from Y to frequency". As I already have a function that converts from frequency to Y, doing it the opposite way is easy to achieve. My problem is getting the actual Y positions. My pipes are generated in a Prefab as a parent GameObject with 3 children:
1) the upper pipe sprite
2) a boxcollider in the middle as the "opening"
3) the lower pipe sprite
Here's a screenshot of the positions that I need to save in my database: https://prnt.sc/nolosx
hitFreq (1) would be easy to access as I can just use the player's transform.position.y
But I have no idea how I'm supposed to access maxFreq (2) and minFreq (3). I can get the position of the parent Prefab object, but I don't know how I can calculate the positions of the upper edge and lower edge of the opening. I'd appreciate if someone could help me find a solution!
Your issue then is knowing number 2 and 3 position? You could add childs objects at the upper and lower edge and calculate the current when you trigger the box collider, or when you trigger the collider use 2 raycast upwards and botton and compare collision position
This is my first application using Unity, so I don't really know how to apply your solutions :( I followed a tutorial for the code that I have now... I understand the code that I have, but my knowledge is limited to that
Hi @xxmariofer, I'm sorry for insisting, but this is an app for my bachelor thesis so it's very important... I have looked at what Raycasts are and it seems like it could be a solution, but I need help on how to actually use them
without knowing your code is hard to say but something like this could help
RaycastHit2D hit1 = Physics2D.Raycast(transform.position, Vector2.up);
// If it hits something...
if (hit.collider != null)
{
float position= $$anonymous$$athf.Abs(hit.point.y);
}
this should be done inside the ontrigger and using the vector2.up/down for getting both points
Your answer
Follow this Question
Related Questions
Copy position from one object to another on one axis 0 Answers
Help! I want to make a following camera but in with Y position is constant ? 2 Answers
Global transform position and scale for gameObject 0 Answers
How do i move a GameObject to the same height as another GameObject? 0 Answers
Accurate placed object's transform messes up on game start 1 Answer