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 /
avatar image
0
Question by miguelrmonreal · Jun 15, 2018 at 08:56 PM · shootingshoottouchscreenturret

How can i set up a turret that turns and shoots where you touch onscreen?,how to shoot where you touch

i'm trying to make it where the turret turns and shoots where you touch on the screen, but i'm not really sure how to do it? ,How can i make a turret turn and shoot where you touch on the screen?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Topthink · Jun 15, 2018 at 09:05 PM

You'll need to use some of the Raycast functions to identify a point where you click on the map. Something like this from one of my programs (you'd need to change it to suit yours).

     private void Update () {
         // on right click...
         if (Input.GetMouseButtonDown(1)) {
             RaycastHit hit;
             // this is an easier way of creating a ray from mouse click
             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) {
                 // instead of checking tag, get the script directly
                 UnitSphere unitClicked = hit.collider.GetComponent<UnitSphere>();
                 if (unitClicked != null) {
                     // change selection, wrapped into a function
                     SelectUnit(unitClicked);
                 }
                 // clicked on something else; you might want to check if it is terrain
                 else {
                     // if there is a unit selected...
                     if (selectedUnit != null) {
                         if (Input.GetKey(KeyCode.LeftShift)) {
                             selectedUnit.AddWaypoint(hit.point);
                         }
                         else {
                             selectedUnit.SetWaypoint(hit.point);
                             // unselect as you do in your example; 
                             // however, it would be better to have selection/unselection on a different button!
                             SelectUnit(null);
                         }
                     }
                 }
             }
         }
     }

Take out the select code that will get the hit.point on the map where you click. After that, you would want your turret object to (probably) "Look At" the hit point and fire away with your projectile hitting in or around the hit.point. You'll probably need to look up how to use "Look at" to accomplish what you need to do. Lots of other ways to do it. But I think this is the easiest.

Comment
Add comment · Show 4 · 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 Topthink · Jun 15, 2018 at 09:07 PM 0
Share

I was using that for a program that sets waypoints for moving objects. There is a lot of stuff in there that you don't need. Let me know if it's confusing at all and I'll cut out all the junk you don't need.

avatar image Topthink · Jun 15, 2018 at 09:15 PM 0
Share

Something like the following will get you the point where you click on the map. It's probably a little easier to understand than what is above. You will still need the turret to rotate toward the target in some manner.

void Update() { if (Input.Get$$anonymous$$ouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { target = hit.point; transform.position = target; } } }

avatar image Topthink · Jun 15, 2018 at 09:21 PM 0
Share

Here's something from the manual for moving a navmeshagent. Ins$$anonymous$$d of moving to the location, you would have the location be your "target"

     // $$anonymous$$oveToClickPoint.cs
     using UnityEngine;
     using UnityEngine.AI;
     
     public class $$anonymous$$oveToClickPoint : $$anonymous$$onoBehaviour {
         Nav$$anonymous$$eshAgent agent;
         
         void Start() {
             agent = GetComponent<Nav$$anonymous$$eshAgent>();
         }
         
         void Update() {
             if (Input.Get$$anonymous$$ouseButtonDown(0)) {
                 RaycastHit hit;
                 
                 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {
                     agent.destination = hit.point;
                 }
             }
         }
     }
avatar image Topthink · Jun 15, 2018 at 09:22 PM 0
Share

The gist of all these posts is that you'll likely need to use some sort of "raycasting" to mark your spot where the turret shoots.

avatar image
0

Answer by miguelrmonreal · Oct 15, 2018 at 02:22 PM

thank you, i will try this.

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

89 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

Related Questions

Launch a projectile from one object to another 1 Answer

Turret shooting at me 1 Answer

How to make a gun in unity? 1 Answer

OnColliderEnter - Turret Shoot Only once problem 1 Answer

Instantiating Muzzle Flash 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