Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Zenithin · Jun 12, 2016 at 08:14 PM · c#scripting probleminstantiate prefabbegginer

Instantiate And move the GameObject to the click position from another position.

So basically I am trying to make a archer and want the arrow to start from archer and go anywhere the user clicks.

So like archer is standing in the bottom left corner of screen and I want a arrow to be initiated at his position and go to the place wherever user clicks with mouse or touch on the screen. I have a arrow prefab.

I am attaching this script to the archer. But there is some issue with the coordinate system .. :( Please help/

----------``

using UnityEngine; using System.Collections;

public class FireArrow : MonoBehaviour { public float speed = 1F; public GameObject arrow; GameObject go; bool moveArrow; // Use this for initialization void Start() { moveArrow = false; }

 // Update is called once per frame
 void Update()
 {
     Vector3 worldPos = new Vector3(0, 0);

     if (Input.GetMouseButtonUp(0))
     {
         Debug.Log("Pressed left click.");
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = 0.0f;      
         worldPos = (mousePos);
         go = GameObject.Instantiate(arrow,transform.position, Quaternion.identity) as GameObject;
         moveArrow = true;
           }

     if (moveArrow)
     {
         if (go.transform.position != worldPos)
         {
              go.transform.position =(worldPos);
             }
     }}
 }






Comment
Add comment · Show 1
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 Zenithin · Jun 12, 2016 at 08:18 PM 0
Share

Should I be ins$$anonymous$$d using something like button with alpha 0 ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lilius · Jul 18, 2016 at 07:59 AM

I'm doing something like that at the moment. I move the arrow with script attached to arrow itself, so I can shoot another arrow before the previous arrow has reached its target. Maybe this works for you too. I originally used to shoot arrows towards the direction I clicked but these scripts shoot the arrow exactly to the point you clicked. Also added a timer between shots. With touch devices this script shot about ten arrows whit quick tap on the screen without the timer.

I use this script on my weapon:

 using UnityEngine;
 using System.Collections;

 public class FireArrow : MonoBehaviour
 {
     public GameObject arrow;
     public float shootTimer = 1;
     public float timeBetweenShots = 1;

     void Update ()
     {
         shootTimer += Time.deltaTime;

         if (Input.GetMouseButtonDown(0) && shootTimer >= timeBetweenShots)
         {    
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

             if (Physics.Raycast(ray, out hit))
             {
                 GameObject instantiatedArrow;
                 instantiatedArrow = Instantiate(arrow, transform.position, Quaternion.identity) as GameObject;
                 ArrowScript arrowScript;
                 arrowScript = instantiatedArrow.gameObject.GetComponent<ArrowScript>();
                 arrowScript.target = hit.point;
                 shootTimer = 0;
             }
         }
     }
 }

And this on my arrow prefab:

 using UnityEngine;
 using System.Collections;

 public class ArrowScript : MonoBehaviour
 {
     public Vector3 target;
     public float speed;

     void Update ()
     {
         float step = speed * Time.deltaTime;
         transform.position =  Vector3.MoveTowards(transform.position, target, step);
     }
 }
Comment
Add comment · 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

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

169 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I get a reference to a script that is on another gameobject 0 Answers

Generating random points and instantiating prefabs with a set distance 0 Answers

Dreamlo Leaderbord issue 0 Answers

Hololens sharing example 1 Answer

How should I use the `FreeLookCam` script located in the standard assets folder 0 Answers


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