I am trying to make a bullet that gets shot when the mouse is clicked but it does not want to instantiate.
This is the script for the player to face the mouse.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FaceMouse : MonoBehaviour {
void Update() {
facemouse();
}
void facemouse()
{
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector2 direction = new Vector2(
mousePosition.x - transform.position.x,
mousePosition.y - transform.position.y
);
transform.up = direction;
}
}
This is for the bullet to shoot
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletScript : MonoBehaviour {
int fireCountdown = 0;
public Rigidbody Bullet;
public float speed = 10f;
void FireRocket()
{
Rigidbody bulletClone = (Rigidbody)Instantiate(Bullet, transform.position, transform.rotation);
bulletClone.velocity = transform.forward * speed;
bulletClone.GetComponent<BulletFiredScript>();
bulletClone.GetComponent<BulletFiredScript>().Fire();
}
void Update () {
if (Input.GetMouseButtonDown(0) && fireCountdown == 0)
{
fireCountdown = 300;
}
else
{
fireCountdown = fireCountdown - 1;
}
}
}
and this is to instantiate the bullet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletFiredScript : MonoBehaviour {
public void Fire()
{
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector2 directionz = new Vector2(
mousePosition.x - transform.position.x,
mousePosition.y - transform.position.y
);
transform.up = directionz;
}
void Update () {
Vector2 pos = transform.position;
pos.x = pos.x + transform.rotation.x;
pos.y = pos.y + transform.rotation.y;
transform.position = pos;
}
}
When the mouse is clicked it does nothing. Please help.