Instantiating Projectiles not working at certain directions??
Hey, I'm new to Unity and C#, and I am creating a simple game for my application. Sorry if some things sound confusing, Im trying my best to explain my problem. I have 2 players moving in a certain direction. I then coded a script attached to a Bullet Emitter (null object) that follows the player and rotates in the direction the players is walking. From this emitter, the player can fire bullets when he presses "2". The emitter then instantiates a bullet prefab and fires it in the direction of the blue axis of the emitter (ergo the direction the player is walking to, because the emitters script). This works fine in every direction, but when I combine directions (like pos. horizontal and neg. vertical), things go crazy. The bullets spawn in most directions, but in some, bullet copies aren't even instantiated. More detailed: firing bullets in following directions works: walking in + / - horizontal , + / - vertical, + horizontal + / + vertical , -horizontal - vertical. +horizontal -vertical doesnt work
Here are the scripts that might be relevant:
Bullet Emitter
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet, Emitter.transform.position, Emitter.transform.rotation) as GameObject;
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
Temporary_RigidBody.AddForce(transform.forward * BulletSpeed);
Destroy(Temporary_Bullet_Handler, 3.0F);
Bullet Emitter Repositioner
public GameObject obj;
public Vector3 offset;
public int player;
private Vector3 xOffset = new Vector3 (0, 0, 0);
private Vector3 zOffset = new Vector3 (0, 0, 0);
void Start () {
offset = obj.transform.position - this.transform.position;
}
// Update is called once per frame
void Update () {
float horizontal = Input.GetAxis("Horizontal" + player);
float vertical = Input.GetAxis("Vertical" + player);
if (horizontal < 0) {
xOffset = new Vector3(-2.5F, 0, 0);
}
else if (horizontal > 0) {
xOffset = new Vector3(2.5F, 0, 0);
}
else if (horizontal == 0) {
xOffset = new Vector3(0, 0, 0);
}
if (vertical < 0) {
zOffset = new Vector3(0, 0, -2.5F);
}
else if (vertical > 0) {
zOffset = new Vector3(0, 0, 2.5F);
}
else if (vertical == 0) {
zOffset = new Vector3(0, 0, 0);
}
if (horizontal == 0 && vertical == 0) {
zOffset = new Vector3(0, 0, 2.5F);
}
this.transform.position = obj.transform.position - offset + xOffset + zOffset;
this.transform.forward = obj.GetComponent<Rigidbody> ().velocity;
}
Hope you can help me!
Answer by SilvanK · Aug 04, 2017 at 11:11 PM
I FOUND THE PROBLEM! It is not the code or any bug with unity, it is the mousepad! Just google "mousepad ghsoting" and you will see what I mean. Basically, it means that some combinations of keys aren't registered due to technical reasons. The more keys, the more likely the combination is to not work. In this case, down + right + numpad2 couldnt be registered (but other arrows + numpad2 did, strangely). Hope there is any fix to the hardware, but that's not of this forum's buisness, I guess...
Answer by Darksorcerer57 · Aug 03, 2017 at 10:49 PM
For the x and z offsets, if you're trying to move it along the x and z axis respectively, then simply adding those values to the Vector3 won't work, you have to add a Vector3 with those values in their respective places to its position or use the translate function on its transform, again with those values in their respective places.
Hey, thank you for your answer. I still didnt quite get it, Im afraid ^^ Just by the way, moving the emitter to the right spot and rotating it correctly works perfectly, I tested it in play mode and watched the emitter in scene mode. There was nothing wrong with the offsets, rather with the bullet instantiation. $$anonymous$$oving in pos. Horizontal and neg. Vertical hinders bullets from spawning, somehow. Every other direction works. I also found out that this is only the case when I am holding the right and down arrow, when I press 2 shortly after I release these buttons (but the character still moves in that direction), bullets do instantiate. Strange problem I know, but maybe someone knows the answer ^^