• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Turret shooting at me 1 Answer

Launch a projectile from one object to another 1 Answer

Shooting and reloading mechanism problem. 1 Answer

shooting problem with raycasting 1 Answer

Shooting at the nearest enemy automatically. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges