Question by
Dan5500 · Jan 19, 2021 at 02:52 PM ·
instantiateprefabvelocityprojectile
Instantiating prefabs, and making them move. (Projectiles)
Hi! I need help with my code. I'm trying to make a script where if you click then a bullet gets shot in the direction of the mouse. (If you can tell me how to move the gun to match the direction of the mouse too, that would be great) My current code makes me think it would work, but whenever I try testing it then it just goes splat. (The bullets fall right off the map) The prefab I'm using has Gravity on, so I don't know if I should use that or not for my bullets.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
public Rigidbody Bullet;
public float speed = 4;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Rigidbody BC = Instantiate(Bullet, transform.position, transform.rotation);
BC.velocity = transform.forward * speed;
}
}
}
Thank you for helping!
Comment