- Home /
Shooting at ground
For some reason I can't shoot normally I just shoot at the ground. And I don't know why.
Here's my shooting script. I just attach it as a component to my character : Mainplayer
Code:
var ammo : int = 220;
var rocketPrefab: Transform; // your rocket prefab
var rocketSpeed: float = 20; // rocket speed
var spawnPoint: Transform; // empty object which defines the spawn point
var rateFire : float = 0.5;
var access : boolean = true;
private var canShoot : boolean;
function Start(){
canShoot = true;
}
function Update () {
ToFire();
if (ammo < 1){
canShoot = false;
}
}
function OnGUI(){
GUI.Label(Rect(15,15,200,100),ammo.ToString());
}
function ToFire(){
if (Input.GetButton("Fire1")){
if (access){
access = false;
//here all the events to fire the gun //START NOW!
if (canShoot){
if (Input.GetButton("Fire1")){
animation.Play("fps_attack_1");
ammo -= 1;
var ray: Ray = Camera.main.ViewportPointToRay(Vector3(0.5,0.5,0));
var hit: RaycastHit;
var dir: Vector3;
if (Physics.Raycast(ray, hit)){
dir = (hit.point - spawnPoint.position).normalized;}
else{
dir = ray.direction;}
var rot = Quaternion.FromToRotation(rocketPrefab.forward, dir);
var rocket = Instantiate(rocketPrefab, spawnPoint.position, rot);
rocket.rigidbody.velocity = dir * rocketSpeed * 5;
WaitForSeconds(1);
}}
//END NOW!
yield WaitForSeconds(rateFire);
access = true;
}
}
}
function OnCollisionEnter(col : Collision){
if(col.gameObject.tag == "AmmoPack"){
ammo +=15;
Destroy (GameObject.Find("AmmoPack"));
}
}
Thanks in advance!
EDIT:
I don't get any errors either.
Answer by Lockstep · Mar 15, 2013 at 04:00 PM
You calculate the rotation of your rocket in a pretty strange way. It is way better to make sure your spawnPoint points in the direction you want to shoot and then set your rocket accordingly:
var rocket = Instantiate(rocketPrefab, spawnPoint.position, spawnPoint.rotation);
rocket.rigidbody.velocity = spawnPoint.forward * rocketSpeed; // what is the 5 for??
Also you check for Input.GetButton("Fire1") twice which is redundant. You can remove the second statement to keep your code clean. Furthermore your OnCollisionEnter is wrong too. You use GameObject.Find to destroy your ammopack. But GameObject.Find will return the first gameObject with name AmmoPack in your scene, which is not neccessarily the one you picked up. Use
Destroy(col.gameObject);
instead. This will destroy exactly the ammoPack you picked up.
:/ it's still doing it... Thanks though for the script.
I just copied your script and it worked just fine. I only commented the animation out since I don't have it.
Is the shotpoint a child of your character? Naturally you want the shotpoint to move with your character.
Have you messed with the gravity? If the gravity is too high you won't notice the velocity you give your rocket in your script and the rocket will just drop. Test this by unchecking the 'use gravity' checkbox of the rocket prefab.
Oh wow.... I feel incredibly stupid now... I just realized that i put the spawnpoint of the missle inside of my character collider XD...Wow. hahah thanks for Helping though! :D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Photon Networking Prefab help 0 Answers
C# scriping help 0 Answers
Can you help me find the script error please? 1 Answer
Not working gui buttons 1 Answer