[I still need help even though there is 2 replies] Help with shooter script
Hi! I just need help with my character shooting script. Right now the bullet spawns somewhere other than from the character's gun. What do I do to fix that. Here are my scripts: Shoot script attached to the player:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Shoot : MonoBehaviour {
public GameObject projectilePrefab;
GameObject projectileInstance;
public float offset;
public float offsetTwo;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.V)) {
Instantiate (projectilePrefab, (transform.position * offsetTwo) + (Vector3.right * offset), transform.rotation);
}
}
}
Script moving the bullet:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ProjectileMove : MonoBehaviour {
public float speed1 = 100;
// Use this for initialization
void move () {
transform.Translate (Vector3.left * Time.deltaTime * speed1);
}
// Update is called once per frame
void Update () {
move ();
}
}
. And Yes! I have tried changing the offsets, many times.
You already have 2 offset variables, have you tried changing them untill the projectile spawns infront of your gun?
Answer by Skaster87 · May 31, 2017 at 12:39 AM
Hi @KingSloth
The first problem I see is in the Update() function when you instantiate you use two offset, and you're multiplying the Vectors inside the parenthesis, was that intentional?
What I would do instead is declare a variable that contains the location you want to spawn at. So on your Gun create an empty GameObject and place the transform at the tip of the gun. In your first script add public Transform firePoint;
then in the inspector drag the GameObject into the slot. After that you can change your instantiate to Instantiate (projectilePrefab, firePoint.position, firePoint.rotation);
Good Luck!
using UnityEngine;
public class Shoot : MonoBehaviour {
public GameObject projectilePrefab;
GameObject projectileInstance;
public Transform firePoint;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.V)) {
Instantiate (projectilePrefab, firePoint.poistion, firePoint.rotation);
}
}
}
Answer by KingSloth · May 31, 2017 at 12:41 AM
@Skaster87 The bullet is being spawned really far away from fire point.
If your certain the firepoint transform is in the correct location, and you dragged it correctly into the script, then I think its your speed variable. Try bringing it down to 1 ins$$anonymous$$d of 100.
Did you try Skaster87's suggestion to use a separate transform for the bullet origin, and remove the offset vectors? If so, try setting the speed to 0 and verify in scene view that the bullets are being instantiated at the same position as the transform you assigned to the Shoot script.
Also, make sure not to ask questions or post comments as answers. Ins$$anonymous$$d, click "Add comment" to the answer you wish to comment on.
Hey guys, thank you so much for your help, I am going to test it right now. The reason I used the reply, is that when I pressed "add comment" yesterday, It just refreshed the page. It is working now though! Thanks again.
Answer by Ark_Revan · May 31, 2017 at 07:33 AM
You need to create an empty game object that will be a child of your gun. Then you put this empty game object in front of the gun, where you want it to shoot. Be careful not to put it inside or your bullet will collide with the gun. Rotate it in the same direction as the gun ( you don't want the bullet to do an angle when you shoot , don't you ?)
Then you add your shooting script to this empty game object. This script need 3 things: 1) A public gameobject that will be used to store your bullet prefab ( there are other way but it's this easiest I can think of ). Take care that this prefab is well done ( especially, reset the position so the object is in the center of ... itself ? not sure how to explain it. but you don't want the bullet to have an offset when it appears. 2) The trigger to fire ( here, your mouse click ) 3) A function to instantiate your bullet. Since you put the script in an empty game object which is near enough from the gun. you only need to instantiate it with the transform.position & the transform.rotation without any cast/call or anything. Then you should give it a velocity along the forward axis.
Your answer
Follow this Question
Related Questions
Switching lanes 1 Answer
Isometric movement for MoveTowards 0 Answers
Why is my bool being set to true when program is run? 1 Answer
How to have a child object camera with gaze move its parent object 0 Answers
Movement input problem? 1 Answer