I attached a video were you can see the problem. I think that i don’t have
discribe the Problem, it’s enough if I show you that video and the sourcecode.
I’ll also attach a image were you can see the structure of my projekt.
Video:
Code:
BulletMove.js - BulletMove.js - Pastebin.com
BulletSpawn.js - BulletSpawn.js - Pastebin.com
Shoot.js - Shoot.js - Pastebin.com
VanishStartBullet.js - VanishStartBullet.js - Pastebin.com
Image:
http://fs2.directupload.net/images/141220/ibozuide.png
Why do you have so many scripts?
As far as shooting a gun, at most you should have two. Gun_ Controller and Bullet_Unit.
Gun_Controller will monitor input, on fire btn pressed, fire(), here you instantiate, or call from a pool, a Bullet_Unit. Set it the same rotation, position as barrel of gun. And then fire the bullet. Here is where the bullet takes over, propelling itself.
Is it possible to create a reference to the bullet i’ve created, that i can say
bullet.transform.position.x = 0; or something like this?
If you are instantiating and destroying the bullet, I would suggest you have no need to reference it, however yes.
GameObject bullet = Instantiate();
///now you could add this bullet to a List<> for example
If you are pooling your bullets, you should already have a reference for it.
this:
// This funktion fires the bullet and sets it's roation, scale and position.
function fire()
{
// creates a reference to bullet
var b = Instantiate(bullet);
// sets position of bullet to spawnposition
b.transform.position.x = transform.position.x;
b.transform.position.y = transform.position.y;
b.transform.position.z = transform.position.z;
// sets rotation of bullet to spawnposition
b.transform.rotation.x = transform.rotation.x;
b.transform.rotation.y = transform.rotation.y;
b.transform.rotation.z = transform.rotation.z;
// adds bullet to bulletList
bulletList.Add(b);
}
is now my fire funktion. but it don’t work! No SyntaxError but it don’t work. Now the bullet never shoots, not in the false direction.
here is my full code:
Gun_Controller.js - Gun_Controller - Pastebin.com
Bullet_Unit.js - Bullet_Unit.js - Pastebin.com
i’ve also added a few comments.
Edit: no thats wrong. It shoots, but the bullet is nowere the position is false, but why?
first, why write,
// sets position of bullet to spawnposition
b.transform.position.x = transform.position.x;
b.transform.position.y = transform.position.y;
b.transform.position.z = transform.position.z;
// sets rotation of bullet to spawnposition
b.transform.rotation.x = transform.rotation.x;
b.transform.rotation.y = transform.rotation.y;
b.transform.rotation.z = transform.rotation.z;
Just write.
b.transform.position = transform.position;
b.transform.rotation = transform.rotation;
Second, you dont do anything to your bullet. You set up no way for it to propel itself. You must use Translate(), MoveTowards() or some type of Physics Force to it.
ok now i’ve changed my code like this:
// This funktion fires the bullet and sets it's roation, scale and position.
function fire()
{
// creates a reference to bullet
var b = Instantiate(bullet);
// sets position of bullet to spawnposition
b.transform.position = transform.position;
// sets rotation of bullet to spawnposition
b.transform.rotation = transform.rotation;
// adds bullet to bulletList
bulletList.Add(b);
}
but it didn’t work. so first i make the movement with Translate():
function Update () {
// Let the Bullet move forward. (Here is a mistake)
transform.Translate(Vector3.forward * Time.deltaTime);
//transform.position = transform.position + transform.forward * distance * Time.deltaTime;
...
i am not sure if it’s working, because i can’t see the bullet.
How to change the variables from “b”? how to reference it right?
Add in a factor to slow or speed the bullet up. Also, place a print in at start, just to see if it is created and in the correct position or watch the hierarchy to see if it is there.
EDIT:
Now I’ve changed the most part of the code.
I’ve attached it.
Gun_Controller.js - Gun_Controller - Pastebin.com
Bullet_Unit.js - Bullet_Unit - Pastebin.com
The Problem is now, that i can’t set the position of the bullet in my Gun_Controller file. It still not work, I’ve tried much ways but none worked.