Firstly you should fix your Instantiation. That will spawn a new bullet every single frame you’re holding down left click. Use GetKeyDown or GetMouseButtonDown just for the first click, or add a delay before spawning a new one.
To make the bullet move, maybe add a Bullet script to it that moves it in its forward vector on Start.
Yeah just noticed the bullet spawn madness. How would I go about adding a delay and movement for the bullet? Im kinda just slapping these things together as I go.
EDIT: Nvm on the delay the Getkeydown is working fine for what I want.
Can anyone help me with getting my bullet to move when spawned? I have been struggling for days trying different things but I just cant seem to get it to work.
Will give it a shot but wont the rigidbody collide with the player as it gets spawned on the player? Im at work at the moment will check when I get home. I just realized I forgot to mention its a 2D top down shooter
Create a:
Player layer ( The player will be in this layer)
Projectiles layer ( All player shoot projectiles go here)
and a enemys layer ( I guess you have enemys if you shoot so)
Now program it so that
player collides with projectile = nothing happens.
enemy collides with projectile = enemy dies ( or takes dmg or whatever you want).
enemy collides with player = player gets dmg.
okay so this is beginning to frustrate me so much Not quiting though. So I modified the script to Zaflis suggestion and its still not working Posting screenies and shizz.
So the script atm:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 5.0f;
public GameObject bullet;
// Use this for initialization
void Start () {
}
private void Awake(){
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.Mouse0)){
GameObject b = Instantiate(bullet, transform.position, transform.rotation);
b.GetComponent<Rigidbody>().AddForce(transform.forward * 1.234f);
}
if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.Q)){
transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
} else if (Input.GetKey(KeyCode.D)){
transform.position += new Vector3(+speed * Time.deltaTime, 0, 0);
} else if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.Z)){
transform.position += new Vector3(0, +speed * Time.deltaTime, 0);
} else if (Input.GetKey(KeyCode.S)){
transform.position += new Vector3(0, -speed * Time.deltaTime, 0);
}
{
Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
}
Le bullet:
Le player:
So I have no idea what im doing wrong. LeftyRighty help a brother out
ah in my frustration I must have skipped to say what isnt working. Okay so the bullet isnt moving at all, it spawns and then it just stays there. Its a 2D Top Down Shooter the Player rotates by following the mouse and the bullet also rotates based on the players rotation. At the moment the only thing not working is the bullet not moving at all.
If it’s 2D you need to be using RigidBody2D’s and all Collider…2D’s in code and inspector. Try bigger force, i tried to make the number 1.2345… look like it’s just a rough example. Test different numbers like 100 or 1000. Then debug all collisions it makes with anything.
(And as he said, forward vector may not be good in 2D if it’s meaning depth axis.)