- Home /
Shoot in the direction mouse is pointing
So i am trying to make the player shoot in the direction the mouse is pointing from my player. i have writen some code that gets me the angle. but i dont want the player to shoot at the mouse only straight up, left and right. like in the video below. https://streamable.com/iy14yr
private void FixedUpdate()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
Debug.Log(rotationZ);
}
the joystick im moving around is giving out values from -1 to 1 which seems easier but i dont know how i could make the screen cordinates into that. but let me know if you have a solution for that
Answer by kirbygc00 · Apr 15, 2020 at 05:45 AM
the joystick im moving around is giving out values from -1 to 1 which seems easier but i dont know how i could make the screen cordinates into that. but let me know if you have a solution for that
You are correct that this is a fairly easy solution. Your joystick will actually give you 2 values, one for x and one for y. Each of these will go from -1 to 1... The -1 on the Y axis, for example, represents pulling the stick down. I would map each of these values to a direction your character can shoot in (except for the -y value).
yeah that was what i was thinking but how would i make screen cordinates into that? i dont want the joystick to actually be there because then i have to click on the character and also it would probably be from the screen middle not from where the character is.
Unity was not letting me post my response so i broke it into 2
yeah that was what i was thinking but how would i make screen cordinates into that?
So basically what your script is doing is setting the rotation for some object which you would spawn. Also, this rotation is not clamped to 0, 90, or 180, so it will not line up with the direction you are ai$$anonymous$$g in.
You still need to find which direction to shoot in, as well as define an offset away from the player to spawn the object.
I wrote a small script which uses the mouse position to select a predeter$$anonymous$$ed spawn location (relative to the player) and spawn a game object at that location. I'm sure there's a nicer way to do this but, eh. If you are using the game pad you would use that input, rather than mouse position to find the spawn location.
using System.Collections.Generic;
using UnityEngine;
public class FireScript : $$anonymous$$onoBehaviour
{
[SerializeField] private GameObject bullet;
[SerializeField] private readonly float offset = 3f;
private void FixedUpdate()
{
// when the player left clicks spawn a bullet in the correct location
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
var mousePos = Find$$anonymous$$ousePos();
var (spawnPos, spawnRot) = FindSpawnTransform(mousePos);
Instantiate(bullet, spawnPos, spawnRot);
}
}
// Assumes your game takes place on a plane with a z of 0
private Vector3 Find$$anonymous$$ousePos()
{
var mousePos = Camera.main.ScreenToWorldPoint(
new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
0));
mousePos.z = 0;
return mousePos;
}
// Stores a list of spawn locations / rotations... should really be an array of a custom class but I was lazy
private static readonly Vector3 UP = new Vector3(0,1,0);
private static readonly Vector3 BACK= new Vector3(-1,0,0);
private static readonly Vector3 FORWARD= new Vector3(1,0,0);
private static readonly Dictionary<Vector3, float> dirs = new Dictionary<Vector3, float>() {{UP, 90}, {BACK, 180f}, {FORWARD, 0f}};
// See which spawn location is closest to mouse cursor when the player clicks
// return a rotation and vector 3 associated with that point.
private (Vector3 spawnPos, Quaternion spawnRot) FindSpawnTransform(Vector3 mousePos)
{
Vector3 closest = new Vector3(0, 0, 0);
var rotationZ = -1f;
foreach (var dir in dirs)
{
var isCloser = Vector3.Distance(mousePos, dir.Key) <
Vector3.Distance(mousePos, closest) ||
closest == Vector3.zero;
if (isCloser)
{
closest = dir.Key;
rotationZ = dir.Value;
}
}
var spawnPos = (transform.position + (closest * offset));
var spawnRot = Quaternion.Euler(0f, 0f, rotationZ);
return (spawnPos, spawnRot);
}
}
Hope that helps.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
parallax on x, But how on the y as well? [Solved] 1 Answer
How to know if my player is not moving? 2 Answers
Movement Problems PS4 Controller 0 Answers