- Home /
Question by
TwoMenArmy · Jul 21, 2019 at 10:34 PM ·
2d gameshooting2d-gameplay
Shooting/Firing in 2D Top Down Shooter
Hi, I'm a begginer and I'm at the part of doing the shooting mechanics, but I'm having a problem! I'm stuck in the part where I shoot the bullets in the direction of the crosshar, but I'm having some problems in the script, where the "player" does not exist in current context. Can you help me?
Crosshair script:
using System; using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CrossHair : MonoBehaviour {
public GameObject Crosshair;
public GameObject BulletVirus;
void Start()
{
}
void Update()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
Vector2 shootingDirection = new Vector2(player.GetAxis("AimHorizontal"), player.GetAxis("AimVetical"));
{
if (Input.GetButtonDown("Fire1"))
BulletVirus = Instantiate(BulletVirus, transform.position, Quaternion.identity);
BulletVirus.GetComponent<Rigidbody2D>().velocity = shootingDirection;
}
}
}
BasicMovement script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BasicMovement : MonoBehaviour {
public Animator animator;
public GameObject Crosshair;
void Update()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Magnitude", movement.magnitude);
transform.position = transform.position + movement * Time.deltaTime;
}
}
thanks for the help!
Comment
I don't see a variable for player. Have you made a variable for it?
Your answer
![](https://koobas.hobune.stream/wayback/20220612205247im_/https://answers.unity.com/themes/thub/images/avi.jpg)