This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour {
private Vector3 mousePosition;
public float offset;
public float movementSpeed;
public Rigidbody2D bullet;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Vector2 target = Camera.main.ScreenToWorldPoint( new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y) );
Vector2 myPos = new Vector2(transform.position.x,transform.position.y + 1);
Vector2 direction = target - myPos;
Rigidbody2D projectile = Instantiate (bullet, transform.position, transform.rotation, null);
projectile.GetComponent<Rigidbody2D> ().velocity = direction * 10;
}
}
}
I’m making a 2D tank game which this player is controlled by the mouse. I’m trying to get it so the bullet goes towards the mouse position. For a long time the bullet was not moving because I disabled RigidBody2D on the bullet prefab… but now when I shoot the bullet falls down with gravity! How do I disable gravity in 2D, and how can I get the bullet to shoot straight? I’ve been trying to figure this out for two days now.