Search code examples
androidunity-game-enginecameracoordinatesaugmented-reality

How can I spawn an object in front of the camera unless the position and direction of my device?


I'm creating a AR game with ARfoundation where the player can swipe a ball to whatever direction.

The problem I encounter is that when the ball is spawned the first time all the other balls that are spawned, will load on the same position as the first ball whatever direction I'm aiming at with my camera.

I want it to get it always spawned in front of the camera no matter the location, position and direction of my phone's camera. Tried using tags to get the location of the camera en the cup of where the ball need to get thrown in but still the ball doesn't get loaded in front of the camera no matter the location. I guess this is not the right way to achieve this and I was wondering what is?

The code I'm using for spawning the ball on a certain location

[SerializeField]
GameObject ball;

public float distanceX;
public float distanceY;
public float distanceZ;

public float ballX;
public float ballY;
public float ballZ;

public void Spawn()
{
    distanceX = GameObject.FindGameObjectWithTag("Cup").transform.position.x - GameObject.FindGameObjectWithTag("MainCamera").transform.position.x;
    distanceY = GameObject.FindGameObjectWithTag("Cup").transform.position.y - GameObject.FindGameObjectWithTag("MainCamera").transform.position.y;
    distanceZ = GameObject.FindGameObjectWithTag("Cup").transform.position.z - GameObject.FindGameObjectWithTag("MainCamera").transform.position.z;

    ballX = distanceX / 4;
    ballY = distanceY / 4;
    ballZ = distanceZ / 4;

    Instantiate(ball, new Vector3(ballX, ballY, 10f), Quaternion.identity);
}

Solution

  • A bit hard to tell how your camera and this Cup Object are related and where exactly your object shall be spawned.

    But in general for spawning something in front of your main camera you can do e.g.

    public void Spawn()
    {
        // this basically does FindObjectWithTag("Main camera")
        var camera = Camera.main.transform;
       
        Instantiate(ball, camera.position + camera.forward * 10f, Quaternion.identity);
    }
    

    If you need a different distance then 10 e.g. based on that Cup object you just replace that or add another offset vector.

    Either way you should not store it in individual floats just to build a new vector again ;)

    E.g. instead of

    distanceX = GameObject.FindGameObjectWithTag("Cup").transform.position.x - GameObject.FindGameObjectWithTag("MainCamera").transform.position.x;
    distanceY = GameObject.FindGameObjectWithTag("Cup").transform.position.y - GameObject.FindGameObjectWithTag("MainCamera").transform.position.y;
    distanceZ = GameObject.FindGameObjectWithTag("Cup").transform.position.z - GameObject.FindGameObjectWithTag("MainCamera").transform.position.z;
    
    ballX = distanceX / 4;
    ballY = distanceY / 4;
    ballZ = distanceZ / 4;
    

    you would rather do

    Vevtor3 distance = GameObject.FindGameObjectWithTag("Cup").transform.position - GameObject.FindGameObjectWithTag("MainCamera").transform.position;
    
    Vector3 ballDelta = distanceX / 4f;
    ...