Hello there,
I wanted to make a 2d top down game where if you press left mouse button 1, then you will shoot a bullet.
the problem is that the bullet is spawned in the middle of the player and it does not even fly in a direction.
Here is my player script :
using UnityEngine;
using System.Collections;
public class PlayerMovement2 : MonoBehaviour {
Rigidbody2D PlayerBody;
Animator Animi;
// Use this for initialization
void Start () {
PlayerBody = GetComponent<Rigidbody2D>();
Animi = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (movement_vector != Vector2.zero)
{
Animi.SetBool("Walking", true);
Animi.SetFloat("Input_x", movement_vector.x);
Animi.SetFloat("Input_y", movement_vector.y);
}
else
{
Animi.SetBool("Walking", false);
}
PlayerBody.MovePosition(PlayerBody.position + movement_vector * Time.deltaTime);
}
}
and here is my bullet script :
using UnityEngine;
using System.Collections;
public class BulletPrototype1 : MonoBehaviour
{
public float maxSpeed = 25f;
public Rigidbody2D bullet;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 1))) as Rigidbody2D;
bulletInstance.velocity = transform.forward * maxSpeed;
}
}
}
I really do not see the problem. Can someone explain the problem to me, and maybe give an example about how it should be done?
I am thinking it is something with the variable and the transform.forward, but i can be wrong about that.
Thank you in advance,
Daniel.
Here is a script that spawns and fires a bullet. I used it for my 2D top down space shooter game i’m making.
using UnityEngine;
using System.Collections;
public class ProjectileSpawn : MonoBehaviour
{
public GameObject Projectile;
private GameObject cloneProj;
public float fireSpeed;
public float fireRate;
public Transform Player;
public float speed;
public Vector3 target;
public Transform target1;
void Start ()
{
target = target1.transform.position;
transform.rotation = Player.transform.rotation;
}
void Update ()
{
transform.rotation = Player.transform.rotation;
if (Input.GetKey(KeyCode.Mouse0) && Time.time > fireRate)
{
fireRate = Time.time + fireSpeed;
cloneProj = (GameObject)Instantiate(Projectile, transform.position, transform.rotation);
target = target1.transform.position;
}
var delta = speed * Time.deltaTime;
cloneProj.transform.position = Vector3.MoveTowards(cloneProj.transform.position, target, delta);
if (cloneProj.transform.position == target)
{
Destroy(cloneProj.gameObject);
}
}
}
Erdroy
2
@Daniel_Zabojca
There is a problem with collision.
Try to ignore collision between player and bullet.
You should use this:
if (Input.GetButtonDown("Fire1"))
{
Rigidbody2D bulletInstance = Instantiate(bullet, transform.position, Quaternion.Euler(new Vector3(0, 0, 1))) as Rigidbody2D;
bulletInstance.velocity = transform.forward * maxSpeed;
Physics2D.IgnoreCollision(bulletInstance.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
Also you can set the rotation when instantiating to current player rotation. (Quaternion.Euler(new Vector3(0, 0, 1)) to transform.rotation, or just zeroe’ this with: Quaternion.identity)