Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ganesh.pingale · Jan 16, 2013 at 02:57 PM · instantiatedestroy

How to Instantiate object on mouse click at mouse cursor position?

I want to instantiate a arrow as a gameobject where i click and destroy arrow when another mouse click and instantiate on another mouse position just like strategy game.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image danilonishimura · Jan 16, 2013 at 03:35 PM 0
Share

If your game is a first person game, you can add a GameObject to your camera hierarchy and use it's transform as source position for the arrow instance.

If you're creating a strategy game and you want to instantiate a object on the mouse position, you can do a Physics.Raycast from the camera position to your mouse position and find out where the ray collided, get the collision point and instantiate the object there. On the Raycast page you'll find an example script showing how to raycast from the camera to the mouse position.

avatar image ganesh.pingale danilonishimura · Jan 16, 2013 at 06:18 PM 0
Share

I will check. Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Jan 16, 2013 at 03:30 PM

"Instantiate...where I click" is a bit complex in Unity because you are mapping your screen into a 3D space. Here is a starter script. Attach it to an object. The object will move to where you clicked. Note the oject is placed at 0.0 on the Z axis. Place this on an empty game object and you can modify it to Instantite game objects at a position.

 using UnityEngine;
 using System.Collections;
 
 public class MoveObject : MonoBehaviour {
 
     Plane plane;
 
     void Start ()
     {
        plane = new Plane(Vector3.forward, Vector3.zero);
     }
 
     void Update ()
     {
        if (Input.GetMouseButtonDown (0))
        {
             Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
                float fDist = 0.0f;
                plane.Raycast (ray, out fDist); 
                transform.position = ray.GetPoint (fDist);
        }
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ganesh.pingale · Jan 16, 2013 at 06:17 PM 0
Share

Thanks for reply but I am working on 2D game where orthographic camera is used. In game my player move where i click but I want to instantiate a arrow as gameobject that shows where player is going to move. Your script is very good but i want according to above. Thanks Again.

avatar image robertbu · Jan 16, 2013 at 06:43 PM 0
Share

The above script will do the job with just a couple of $$anonymous$$or changes. Note there if is only a single arrow that disappears when the character reaches the destination, you might be better off just moving/hiding/showing the arrow rather than creating and destorying the arrow.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't seem to destroy instantiated objects. 1 Answer

Can a Network.Instantiate()'d object be Object.Destroy()'d? 0 Answers

Checking Instantiate/Destroy has been called | Checking number of scene GameObjects 1 Answer

Instantiate with Prefabs 2 Answers

Destroy last placed object and put a new one? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges