Forum rules - please read before posting.

How do I create an object at the position where I click on screen?

I have a close up of the surface of a pond and want to play a short sprite animation of a ripple on the surface at the point where I click.  Unfortunately, I can't work out how to do this.  I can get the ripple to appear every time I click but it always appears in the same place and not at the cursor position.

Any suggestions?

Comments

  • edited September 2017
    You'll need a custom script.  This isn't AC-related, and the Unity forums/documentation are a better resource for this kind of question.

    The mouse position can be read with:

    Input.mousePosition

    To convert it to a world position, use:

    Vector3 worldPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);

    You can then position the ripple according to that with e.g.:

    myRippleGameObject.transform.position = worldPosition;


    You can combine the spawning/positioning code with AC to easily limit the region that the ripples appear, e.g. only when clicking on a Hotspot.  This is done by using the OnHotspotInteract event - see the "Custom events" chapter of the Manual for more on how they work.  This event can be used to check if the player clicked on a Hotspot that represents the pond, and only then spawns the ripple.
  • Okay, thanks.  I'll take a look at all that.
  • Thanks for the help, Chris, but I've taken a look and think the scripting is out of my skill zone for the moment.

    Maybe I could emulate what I need by covering the pond surface with lots of small hotspots.  :)
  • using UnityEngine;
    using System.Collections;
    using AC;

    public class PondEvent : MonoBehaviour
    {

        public Hotspot pondHotspot;


        private void OnEnable ()
        {
            EventManager.OnHotspotInteract += OnClickHotspot;
        }
        
        
        private void OnDisable ()
        {
            EventManager.OnHotspotInteract -= OnClickHotspot;
        }


        private void OnClickHotspot (Hotspot hotspot, AC.Button button)
        {
            if (hotspot == pondHotspot)
            {
                Debug.Log ("The pond was clicked on!");
            }
        }

    }

  • It's not complex .
    using UnityEngine;
    using AC;

    public class Ripple : MonoBehaviour {
        public Hotspot hotspot;
        public GameObject myRippleGameObject;
        public float lifeTime;

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

        public void RippleAppear(Hotspot hotspot,Button button)
        {
            if (hotspot == this.hotspot && button == hotspot.GetFirstUseButton()) 
            {
                Vector3 worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                worldPosition.z = hotspot.transform.position.z;
                GameObject go = Instantiate(myRippleGameObject);
                go.transform.position = worldPosition;
                Destroy(go, lifeTime);
            }
        }

        private void OnDestroy()
        {
            EventManager.OnHotspotInteract -= RippleAppear;
        }
    }

    Drag your pond Hotspot and ripple GameObject into the inspector,and set a lifetime of the ripple.
  • Many thanks to both of you for doing these examples but it's still a little over my head, unfortunately.  :(
  • 1.Create a C# script and rename it "Ripple.cs",then paste all my codes into it.

    2.Select one of the GameObject in your scene and add a "Ripple" component to it.

    3.Drag your pond Hotspot into the "hotspot" box.

    4.Drag your "ripple prefab"(may be a sprite) into the "myRippleGameObject" box.

    5.Set a lifetime of the ripple.

    6.Now when you click your pond Hotspot,a ripple will appear.
  • Thanks for the further help.  I have two (possibly stupid) further questions.

    Can I create the C# script anywhere?
    How on earth do I edit the file in order to paste in the code?
  • You can right-click in the Project window to create new C# scripts.  They should have the same name as the class - "Ripple" in @cnppncnpc's case.  Files can be edited by double-clicking them.
  • You can create a script anywhere in your project,to do this, right click on the project window,choose "Create - C# Script",and then rename it to "Ripple".

    After creating the C# script,you can double click to open and edit it.
  • I double click it and nothing happens.
  • I found the file in the relevant directory and opened it using Wordpad.  Will try to get it working now.
  • Thank you so much.  It now works splendidly.  :D
  • If you're interested, here's my little interactive vignette.

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.