- Home /
Shooting with a problem, please help
Everything was working fine, but then I probably did something and now sometimes when shooting the block don't have force and don't move forward, it spawns in front of me and falls.
The code:
var bulletPrefab:Transform;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
//Create bullet
var bullet = Instantiate(bulletPrefab, GameObject.Find("spawnPoint").transform.position, transform.rotation);
//Direction of bullet and add force
bullet.rigidbody.AddForce(transform.forward * 4000);
}
}
If you need a screenshot of something please tell me.
Thank you.
I found out that it works fine if the box collider is set to 0,1,1. But I need it to be 1,1,1. How can I make that without any problem? I really need this. Thank you.
Answer by AlucardJay · Jul 24, 2012 at 03:34 PM
Your box collider is probably intefering with the player collider. Try Instantiating just a little bit infront of the player (you should also store a reference to the spawnPoint at the Start, then use that instead of finding it every bullet) :
var bulletPrefab : Transform;
var bulletSpawn : GameObject;
function Start ()
{
bulletSpawn = GameObject.Find("spawnPoint");
}
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
//set position
var bulletSpawnPosition : Vector3 = bulletSpawn.transform.position + transform.forward;
//Create bullet
var bullet = Instantiate(bulletPrefab, bulletSpawnPosition, transform.rotation);
//Direction of bullet and add force
bullet.rigidbody.AddForce(transform.forward * 4000);
}
}
Answer by DESTRUKTORR · Jul 24, 2012 at 03:28 PM
transform.forward
is referencing the game object that this script is attached to. Try using bullet.transform.forward
Your answer
Follow this Question
Related Questions
Aiming-recoil animation problem 0 Answers
Rayshoot Reload Problem (Can still shoot) 2 Answers
FPS Shoooting Problem 1 Answer
When I kill one enemy, they all die? 0 Answers
Crosshair Prob 1 Answer