Im making a 2d platformer with basic object throwing but when the object is shot it just falls to the ground like the AddForce isn’t working on it. Plz help
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class Movement : MonoBehaviour
{
public float speed = 6.0f;
public float bulletSpeed = 1000.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float shotInterval = 0.5f;
public Rigidbody bulletPrefab;
private float shootTime = 0.0f;
private Vector3 moveDirection = Vector3.zero;
private bool grounded = false;
private Transform bulletSpawn;
void Start(){
bulletSpawn = transform.Find ("bulletSpawn");
}
void FixedUpdate()
{
if (grounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
CharacterController controller = (CharacterController)GetComponent(typeof(CharacterController));
CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
if (Input.GetButton ("Fire1"))
{
if (Time.time >= shootTime)
{
Rigidbody bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation) as Rigidbody;
bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
shootTime = Time.time + shotInterval;
}
}
}
}