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.