Calculating relative position...

I am looking for help working out a C sharp code… I need to calculate game object A’s position, relative to Game object B’s position AND the way Game object B is facing…

Game object B will always be stationary and facing The same direction…

Thanks…

I am new to coding and trying to learn…

I expect to have to assign a script onto Game object B… The script must look for game object A and relay it’s location relative to where game object B is…

If the programming is your problem, start here. Link

If the math is your problem, start learning the basics of linear algebra. You’ll need it a lot for game development.
relative position = endpoint - startpoint

= -

Thanks for the link…

Learning the concepts of how Unity works is my biggest gripe so far :smile:

There are many ways to find a game object. GameObject.Find() is one, but you should do it in the start script.
If you want to know relative position, you just subtract the real position of one from the other. The rotation, every transform has a rotation, so you can make on equal to another. Do searches and do one thing at a time until you get it figured out.

Thanks for the help thus far… Although sadly I am yet to understand the proper way to implement my ideas! I’m sure what I am asking for is ridiculously simple once the foundations are brought together! I shall continue to try…

I imagine the game object I want to track ‘emitting’ it’s location within update…

I Either need to…

  • Apply a script to game object1 that upon update broadcasts its location is such a way that other game objects can search for it… Thus broadcasting its current location (x, y, z) as a public (float?)

Or…

  • Apply a script on to the other game objects that makes them actively look for the position on Game object1…

Either way, The first thing I must ensure is that the location of game object one is public and that other game objects can look for it…

I am completely new to programming, have spent a good number of hours familiarising myself with some of the terminology and features by means of watching some very helpful you tube tutorials… Waiting for something to ‘click’ because I feel like I am really close to stepping that first foot through the door… I have a strong math and Physics background and can imagine the knowledge starting to snow ball…

Can anyone explain in detail what the steps are in the script I am trying to calculate?

Thanks

In the unity Inspector, an objects position is already listed within the Transform tab as an (x,y,z)…

This is one half of my problem without doing anything, right? Unity is already tracking the position of said game object… Thus all I need is for other game objects to look for these values upon update… The I can recalculate those values to be relative (local)

It’s so frustrating for me!

OK - So I am gradually gaining an understanding upon things… I have been learning C#

I have gotten thus far…

1 using UnityEngine;
2 using System.Collections;
3
4 public class HRabbit1Script : MonoBehaviour {
5 float HDefLife = 2.5;
6
7 // Update is called once per frame
8 void Update () {
9 transform.Translate (0f,0f, MissPosition / HDefLife)
10 }
11 }

Now, I know I will need to factor in a time reference, calling this function every frame is not what I need or intend! I can handle that… But still, my problem is as follows… The ‘MissPosition’ (In the z axis part of the script) is referring to the position of another game object named Miss… How can these two objects communicate? Do I parent them? Or by means of accessing other classes…

I know the position of the game object Miss, its within it’s transform component… I just don’t understand the easiest way of ‘subbing’ that info into this other game objects script… I don’t think I can handle using get.component… As performance will be a big factor…

Any pointers for me? Many thanks :smile:

All I need is the easiest way to

Do I need to put a script on the gameobject named Miss which every frame declares its position, or is it already doing that within it’s transform.position component?

You’re doing good so far.

One of the easiest ways to get a reference to another object is to use a public reference. To do this, just declare “public Transform Miss;” in your HRabbit1Script. Then after switching back to Unity, select the object with the HRabbit1Script on it, and you’ll see a new slot in the Inspector called “Miss” that is currently empty. Just drag the Miss object from the hierarchy to the Inspector slot and it will establish the reference. You’ll only need to do that once.

Now you can access that reference in your script. So something like this will now work:
transform.Translate (0f,0f, Miss.position / HDefLife);

To answer your question regarding a “time reference”, what you want is Time.deltaTime. This is always set to the amount of time that the last frame took to complete. Unity - Scripting API: Time.deltaTime

So you would alter your line a little further:
transform.Translate (0f,0f, (Miss.position / HDefLife) * Time.deltaTime);

Now it will be time-dependent instead of frame-dependent. To futher expand on that, you could multiply the Z result by another value that would act as your speed (moveSpeed would be another public float so you could adjust it in the Inspector):
transform.Translate (0f,0f, (Miss.position / HDefLife) * Time.deltaTime * moveSpeed);

Hope that helps.

EDIT: Note that this probably wouldn’t compile as you can’t have a Vector3 inside of another Vector3. Maybe you wanted this (only the Z component)?: transform.Translate (0f,0f, (Miss.position.z / HDefLife) * Time.deltaTime * moveSpeed);

1 Like

Thanks mate, brilliant answer, much appreciated…

Shall try such methods presently…

Regarding the edit… Do you mean…

That within each comer (specifying a single axis), you can only reference that particular axis? (That would make sense)

Ie if you wanted to reference the position of Miss on ALL axis the code would need to look like this…?

transform.Translate (Miss.position.x, Miss.position.y, Miss.position.z)

Causing the scripted object to follow Miss at every update?..

Regarding the acceleration/move speed… Thanks for enlightening me on how that is done… Although for this particular object I wont need that, this object will be an invisible rabbit, thus have no physics, there simply for other objects to chase, Resulting in the illusion of AI…

What I may well need to do though… I slow down the rate at which these values are calculated… Thus slowing down the Time.delta.Time… for different objects giving the illusion that some objects are Sometimes (depending upon how soon the update is called after an event) slower at reacting than others :wink: Love it…

Thanks again

Peace

And I feel the reasoning of why this is so… “public Transform Miss” - You are stating that the Transform (position) of object “Miss” is a public variable within the script…?

Brilliant it has worked thanks… Now I need to research why it is continuing to move everyframe! It should not move the second frame because the position of Miss is the same as in frame 1!!! It has reached its destination so to speak… Hmmmm!

Is transform.translate adding an acceleration then? Because I was expecting it to move TO the position in the gameworld along the z axis… Instead it continuously travels along it… at a rate of “(Miss.position.z / HDefLife) 8 Time.deltaTime” I presume!?

Glad to hear you got it working!

Your first question yes, setting all 3 values would work as well, you could even just pass in the Vector3 (transform.Translate(Miss.position)). However, see below.

transform.Translate will move the object instantly by the amount that you pass in. So with your code yes it will move every frame because you are telling it to move by a factor equal to the Miss object position. If the Miss object is at position 0,0,5 you are telling your other object to move 5 units (lessened by multiplying by Time.deltaTime) in Z every frame. Unity - Scripting API: Transform.Translate

If you simply want your HRabbit1Script object to match the position of the Miss object every frame, just do this:
transform.position = Miss.position;

Hope that helps.

1 Like

Ah I see… Translate is a speed/acceleration thing… and position is strictly referring to coordinate positions… Thanks you greatly again… I have learned more in this conversation that I could have hoped for… Bless for the tuition… :smile:

transform.position = new Vector 3 (0, 0, Miss.position.z / HDefLife);

:smile: :smile: :smile: