How can i display the object on/off in the same distance from player when pressing on F ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DroidPosition : MonoBehaviour
{
    public GameObject droid;

    private float distance;

    private void Start()
    {
        distance = Vector3.Distance(transform.position, droid.transform.position);
        droid.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            droid.transform.position = new Vector3(transform.position.x - distance, transform.position.y, transform.position.z);
            droid.SetActive(!droid.activeInHierarchy);
        }
    }
}

I want that each time i display the Droid by pressing on F it will be at the same distance from the player like in the Start.

transform is the player (FirstPersoncharacter with a Camera).

But when i press F the droid is nit in front of the player like in the Start.
This is where the droid position at before running the game.

When running the game and pressing F the droid is not in front of the player but seems like on the player or somewhere else:

Well, when you set it, you are subtracting the distance from x.
Is distance in front of you all that you care about? That would be +z I would imagine.
If you want the exact offset, that would be a Vector3. It sounds more like you just want +z if it’s always straight in front of you. Something like transform.forward * distance. I assume you could have the droid keep the same y and x as the player. Unless it’s up higher or to the side…

1 Like

When i’m doing:

droid.transform.position = transform.forward * distance;

I don’t see the droid at all.

If i’m doing:

droid.transform.position = Vector3.MoveTowards(transform.position, droid.transform.position, distance);

The droid is in the right height but it’s all the time display in it’s original position and not the position from the player camera.

The player is FirstPersonCharacter and i want that if i moved the player camera not the player position only camera to some direction and pressing F display the droid on this camera direction it’s pointing and with the same distance original distance.

And then when the droid is displaying and i move the player camera around the droid should be following the player camera(FirstPersonCharacter camera). Like in the first screenshot no matter what direction the player is facing display the droid in this direction with the same distance and when moving around the player camera move also the droid with it.

Can you make the droid a child of the camera or the player?

1 Like

Right this is working i made the droid child of the camera.

Well, totally cool… :slight_smile: lol
While that easy solution came up, I was off making this… didn’t polish but will post … b/c :slight_smile:

public class DroidTest : MonoBehaviour {

    [SerializeField]
    Transform droid;
    [SerializeField]
    float distance;
    [SerializeField]
    bool droidActive = false;
    [SerializeField]
    Vector3 xyOffset;
    private void Start()
    {
        Vector3 tmp = new Vector3(droid.position.x, droid.position.y, transform.position.z);
        xyOffset = droid.position - transform.position;
        xyOffset.z = 0;
        distance = Vector3.Distance(droid.position, tmp);
        print("Distance = " + distance);
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            droid.gameObject.SetActive(!droid.gameObject.activeInHierarchy);
            if (droid.gameObject.activeInHierarchy)
            {
                droidActive = true;
            }
            else droidActive = false;
        }
        // testing updates without spam! :)
        if(Input.GetKeyDown(KeyCode.N))
        {
            if (droidActive)
            {
                Vector3 newpos = new Vector3(xyOffset.x, xyOffset.y, transform.position.z);
                newpos += transform.forward * distance;
                print("new pos = " + newpos);
                droid.position = newpos;
            }
        }
    }
}
2 Likes

Regardless, glad it’s working for ya :wink: lol

droid.transform.position = transform.forward * distance;

should be

droid.transform.position = transform.position + transform.forward * distance;

transform.forward is a direction, so you’re only moving the droid around the origin as the player spins about.

1 Like