- Home /
C# Basic Shooting Script
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;
}
}
}
}
I don't think rigidbody.rigidbody
should even compile?!?!?
You mean bullet.rigidbody? so i removed the rigidbody part of it, but it does the same thing
Is there anything else that influences the bullet?
Only one other but it wouldn't affect anything
using UnityEngine;
using System.Collections;
public class Destroy : $$anonymous$$onoBehaviour {
void Start () {
Destroy(transform.gameObject, 5);
}
}
Answer by AusieJamster · Oct 22, 2013 at 04:40 AM
I found that it was shooting into the map and therefore appearing to just fall when it was shooting straight sorry to waste your time :(
Answer by SquidSquad · Oct 22, 2013 at 04:21 AM
Remove gravity from rigidbody, also if the bullet Spawn prefab has Vector3.forward as its y axis. Quaternion.identity should fix that, but if it doesn't manually create vector3 to change the euler angles(in instantiation). also since you instintiate as a prefab gravity is auto on. call buletspawn.gravity.enabled = false imediatley after instantiation. hope that helps
I'm trying to make it more of a grenade movement (as he is throwing an object) then a bullet i called it bullet because I'm stupid. I found that it was shooting into the map and therefore appearing to just fall when it was shooting straight sorry to waste your time :(
If the question is answered you should either close it or post a answer yourself and accept that answer. If you don't know how, watch the tutorial video on the right :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to Sync bullet spawn with shoot animation 0 Answers
Bullet not shooting enemy center in unity 1 Answer
Why does the wrong enemy die with this raycast? C# 2 Answers