- Home /
How to Make Character Shoot Where Aiming with Mouse
I've looked around and none of the stuff I've found seem to help me. Here's my code so far. Right now it just shoots to the right. Now how do I adjust my code to make the bullet go where I'm pointing with the mouse?
using System.Collections.Generic;
using UnityEngine;
public class PlayerShooting : MonoBehaviour {
public GameObject projectilePrefab;
private List<GameObject> Projectiles = new List<GameObject>();
private float projectileVelocity;
// Use this for initialization
void Start () {
projectileVelocity = 10;
}
// Update is called once per frame
void Update () {
Shoot();
}
void Shoot()
{
if (Input.GetButtonDown("Fire1"))
{
GameObject bullet = (GameObject)Instantiate(projectilePrefab, transform.position, Quaternion.identity);
Projectiles.Add(bullet);
}
for (int i = 0; i < Projectiles.Count; i++)
{
GameObject goBullet = Projectiles[i];
if (goBullet != null)
{
goBullet.transform.Translate(new Vector3(1, 0) * Time.deltaTime * projectileVelocity);
Vector3 bulletScreenPos = Camera.main.WorldToScreenPoint(goBullet.transform.position);
if (bulletScreenPos.y >= Screen.height || bulletScreenPos.y <= 0)
{
Destroy(goBullet);
Projectiles.Remove(goBullet);
}
}
}
}
}
Your code seems a little more complicated than $$anonymous$$e so I really can't help you fully but the game I'm making is a top down shooter built in 3D the following is my $$anonymous$$ouseLook.cs script and the 2 lines that instantiate and shoot my prefab bullets. maybe by looking at those you can get an understanding of what's happening in $$anonymous$$e and help you out with yours. I hope so at least.
$$anonymous$$ouseLook.cs
using UnityEngine;
using System.Collections;
public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
private Vector3 mouse_pos;
public Transform target; //Assign to the object you want to rotate
private Vector3 object_pos;
private float angle;
void FixedUpdate (){
mouse_pos = Input.mousePosition;
mouse_pos.z = 0; //The distance between the camera and object
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = $$anonymous$$athf.Atan2(mouse_pos.y, mouse_pos.x) * $$anonymous$$athf.Rad2Deg + 270;
transform.rotation = Quaternion.Euler(new Vector3(0, -angle, 0));
}
}
Instantiate
var clone = Instantiate(bullet, bulletSpawn.position, Quaternion.Euler(randomRotation, randomRotation, randomRotation)); //instantiate bullet and call it clone
clone.velocity = transform.TransformDirection(new Vector3(0, 0, Speed)); //Send it on it's way
I hope this helps.
in the $$anonymous$$ouseLook.cs script, I had to add the + 270 to the angle variable to get it to look at my mouse. That may be a similar fix in your script, I don't know.
If you can't tell, I'm a complete nub at this stuff. I'm trying to get good, but that's gonna take a while. Only been at this about a month now.
Your answer
Follow this Question
Related Questions
Sidescroller Bullets Follow Mouse After Being Shot 2 Answers
Optimizing Code 1 Answer
Need help with screen resolution and character size 1 Answer
2D Shooting Prefab 1 Answer