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.
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);
}
}
}
@Skaster87 The bullet is being spawned really far away from fire point.
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:
- 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.
- The trigger to fire ( here, your mouse click )
- 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.