- Home /
Issue with Bullet Movement shooting
Hi , After Hours Coding Finally i got the problem . The Bullet Not Moving in z Direction relative to Camera Z direction . i dont know what is supposed to be wrong with my code . I guess i did right way when make that script . Here is the code : ![alt text you can see my Bullet not Moving along z direction relative of Camera, but aside slightly . I dont know how to Fix it
public class MenembakUjiCoba : MonoBehaviour {
public float fireRate = 0.5f ;
public float bulletSpeed = 100.0f;
public WeaponType weaponType ;
//public AudioSource source ;
public float nextFire = 0.0f;
public Vector3 posisiPeluru ;
public Ray ray ;
public RaycastHit hit ;
// Use this for initialization
void Start ()
{
weaponType = WeaponType.Automatic;
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButton(0))
{
if(weapon == WeaponType.Automatic && nextFire < Time.time)
{
GameObject bullet = Instantiate((GameObject) Resources.Load("Bullet/bullet",typeof(GameObject)),PlaceInstantiate(),transform.rotation) as GameObject;
bullet.rigidbody.AddForce(transform.forward * bulletSpeed);
Debug.Log(Vector3.Dot(transform.forward,bullet.transform.TransformDirection(transform.forward)));
bullet.rigidbody.useGravity= false;
ray =new Ray(transform.forward,bullet.transform.forward);
if(pel.collider.Raycast(ray,out hit,0.0f))
{
if(hit.collider.CompareTag("Enemy"))
{
Debug.Log("Hit Musuh");
Destroy(pel);
}
}
nextFire = Time.time + fireRate;
}
}
}
public enum WeaponType
{
Automatic=0,
Manual=1
}
public Vector3 PlaceInstantiate()
{
return Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2,Screen.height/2,Camera.main.nearClipPlane ));
}
}
Hi Thanks for Comment before :) I set my Editor on Global. In my First project i get seuccessfully to do that. I dont know what is the Problem
change transform.forward to vector3.forward im guessing im not sure I under stand the script you are using as the instantiate is nothing like I've seen before
i change the transform.forward to be Vector3.forward . But the result is still same. About Instantiate , it just take Object from Assets folder and then Instantiate it in order the object could be seen in my scene. Thanks for your replies anyway ...after some hours someone reply my Thread , hahaha
Your answer
Follow this Question
Related Questions
Raycasr in my fps? 1 Answer
How do i adjust the projectile path and direction? 1 Answer
Rigidbody bullets won't spawn at gun barrel... 2 Answers
FPS rigidbody bullets not moving to center of screen 0 Answers
How to get bullets to hit crosshair 2 Answers