Forum rules - please read before posting.

Dropping Object at Mouse Click Point

edited May 2019 in Technical Q&A

I referenced this: https://adventurecreator.org/forum/discussion/6680/how-do-i-create-an-object-at-the-position-where-i-click-on-screen

I modified the script and have gotten it to mostly function, except that my worldPosition keeps returning to 0.0,0.0,0.0, no matter where on the hotspot I click. Here is the code:

using UnityEngine;
using AC;

public class PlaceCalc : MonoBehaviour
{
    public Hotspot hotspot;
    public GameObject myCalcGameObject;

    private void Awake()
    {
        EventManager.OnHotspotInteract += CalcAppear;
    }

    public void CalcAppear(Hotspot hotspot, Button button)
    {
        if (hotspot == this.hotspot)
        {
            Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            worldPosition.z = hotspot.transform.position.z;
            Debug.Log(worldPosition);
            GameObject go = Instantiate(myCalcGameObject);
            go.transform.position = worldPosition;
        }
    }
}

Any ideas what I'm doing wrong?

Comments

  • I should add that, in addition to this, I need this script to only take effect when using a particular (Cellular Calculator) inventory item. Right now it's firing every time I interact with the hotspot.

  • I transferred this to an action, so I've taken care of the inventory aspect. But I'm still getting 0.0,0.0 for my x and y coordinates here too.

  • This is a Unity question, since the problematic code relies on Input.mousePosition - not anything AC-related.

    You likely need to pass the z-depth between the hotspot and the camera in ScreenToWorldPoint, rather than force the z-position, i.e.:

    float dist = Vector3.Distance (hotspot.transform.position, Camera.main.transform.position);
    Vector3 screenPosition = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, dist);
    Vector3 worldPosition = Camera.main.ScreenToWorldPoint (screenPosition);
    

    See Unity's docs for more.

  • The problem was that I was using Perspective projection instead of Orthographic. It's fixed now!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.