- Home /
what is the wrong,making shoot system?
hello
I want to make shoot system ,i have made a script but the bullet go in one direction only i want the bullet go with the gun in all direction when the gun move
thanks,
using UnityEngine; using System.Collections; public class Shoot : MonoBehaviour { public GameObject bullet; public Transform spawnPoint; public float speed = 50; public float rate = 1; private bool canShoot = true; //-------------------------- //-------------------------- public void ShootBullets(){ //shoot at every rate second(defulat every 1 sec) if(canShoot){ canShoot = false; Invoke("Reset", rate); GameObject bulletClone = (GameObject)Instantiate(bullet, spawnPoint.position, Quaternion.Euler(90, 0, 0)); bulletClone.rigidbody.velocity = (-spawnPoint.up * speed) + rigidbody.velocity; } } //---------------------------- //----------------------------- private void Reset(){ canShoot = true; } }
Answer by Krey · Oct 25, 2013 at 12:43 AM
I believe the problem is a combination of the answer above and the rotation you are setting. As your code stands, you are setting your rotation as
Quaternion.Euler(90, 0, 0)
No matter what. That means that -spawnPoint.forward will always be the same direction (the projectile is always spawned in the same rotation / facing).
I think you're going to want to spawn your projectile more like this (EXAMPLE):
GameObject bulletClone = (GameObject)Instantiate(bullet, spawnPoint.position, FiringWeapon.transform.rotation);
bulletClone.rigidbody.velocity = (-spawnPoint.forward * speed) + rigidbody.velocity;
You're going to need a reference to the weapon firing the projectile (which you'll probably find useful later for other reasons as well).
Answer by Dave-Carlile · Oct 24, 2013 at 08:55 PM
I believe you want to change this:
bulletClone.rigidbody.velocity = (-spawnPoint.up * speed) + rigidbody.velocity;
to this:
bulletClone.rigidbody.velocity = (-spawnPoint.forward * speed) + rigidbody.velocity;
The Transform.forward property points in the direction the object is facing.
What are you setting spawnPoint
to? That should be set to the position of the gun.