- Home /
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?
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.
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.
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; } } }
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;
}
}
}
}
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.
Your answer
Follow this Question
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