- Home /
2D Aiming in a 3D game
I am trying to make a fighting game similar to smash except each characters abilities are set on a deck. I am in the prototyping phase and can't get the aiming or shooting of projectiles to work. The game is 3D but I have locked movement in the z-direction to simulate 2D controls. The player has an empty gameobject at its center with a firepoint child object. The script attached to the empty object is supposed to rotate it and the firepoint with it around the player towards the mouse. However, rotation only occurs if the player moves the mouse position has no effect.
public class FirepointMovment : MonoBehaviour { public float offset = 0f; public float speed = 5f; void Update() { Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; difference.Normalize(); float rotation_z = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rotation_z + offset); } }
The shooting is also not woking as getting a direction relies on the firepoint being in the right spot. here is my current shooting script.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerShoot : NetworkBehaviour {
public PlayerWeapon weapon;
[SerializeField]
private LayerMask mask;
[SerializeField]
GameObject fireball;
[SerializeField]
Transform firePoint;
// Use this for initialization
void Awake ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
private void Shoot()
{
/*
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
Debug.Log(mousePosition);
Vector3 firePointPosition = new Vector3(firePoint.position.x, firePoint.position.y, 0);
Vector3 diffrence = mousePosition - firePointPosition;
diffrence.Normalize();
RaycastHit _hit;
if (Physics.Raycast(firePointPosition, diffrence, out _hit, weapon.range, mask))
{
Instantiate(fireball, firePoint.position, Quaternion.identity);
Debug.Log("We hit " + _hit.collider.name);
Debug.DrawRay(firePointPosition, diffrence, Color.red, 15);
}
*/
Vector3 worldMousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (Vector2)(worldMousepos - firePoint.position);
direction.Normalize();
Debug.DrawRay(firePoint.position, direction, Color.red, 15);
GameObject proj = Instantiate(fireball, transform.position + (Vector3)(direction * 0.5f), Quaternion.identity);
proj.GetComponent<Rigidbody>().velocity = direction * 10;
}
}
Thank you for any help you can give me.