- Home /
Shot is activated only when I walk.
Hello everyone.
I'm trying to understand the problem in this script. When the character walks the shot works but when the character is still the shot is not working.
This is the script:
#pragma strict
var Prefab : Rigidbody2D;
var PrefabSpeed : float = 8;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if(Collisions.GRENADE_AMMO <= 0)
if(!Prefab || !PrefabSpeed)
else
{
if (Input.GetKey(KeyCode.D))
{
var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed);
}
if (Input.GetKey(KeyCode.A))
{
PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(-Vector2.right * PrefabSpeed);
}
}
You are instantiating shots only when either A or D is pressed... Do the same code under fire button.
Thanks for the reply.
You mean like this?
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
}
Update()
{
if(Input.GetButtonDown("Fire1")
{
fire function Instantiate
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)
{
move script right
}
else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)
{
move script left
}
}
Basically what your first code was is that IF you were pressing the mouse button AND pressing the D or A key it would fire.
This way it will fire even without D or A
Answer by barbe63 · May 11, 2015 at 01:41 PM
You have many if without brackets or action assigned on it and you seems to instatiate your bullets in movement key conditions.
It should looks like this:
if(Input.GetButtonDown("Fire1"))
{
//fire action
}
if (Input.GetKey(KeyCode.D))
{
//move right action
}
if (Input.GetKey(KeyCode.A))
{
//move left action
}
Thanks for the reply.
But this is not at work. Now shoot 10 shots all together without me I hit the fire button.
Thank you. I tried to remove the two inputs. D and A. Now Shoot even when stationary. But shows another problem. Shoot simultaneously both right and left, two shots in one.
#pragma strict
var Prefab : Rigidbody2D;
var PrefabSpeed : float = 8;
//var PrefabCreate = Vector2.right;
static var myObject : GameObject;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if(Collisions.GRENADE_A$$anonymous$$$$anonymous$$O <= 0)
{
myObject.SetActive(false);
}
if(!Prefab || !PrefabSpeed)
{
Debug.Log("Il prefab o la velocità non esiste");
}
else
{
//if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed);
//}
//if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
//{
PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(-Vector2.right * PrefabSpeed);
}
Collisions.GRENADE_A$$anonymous$$$$anonymous$$O --;
GameObject.Find("g_Count").guiText.text = ""+Collisions.GRENADE_A$$anonymous$$$$anonymous$$O;
print("YOU NOW HAVE " + Collisions.GRENADE_A$$anonymous$$$$anonymous$$O + " GRENADES");
}
}
}
Answer fully edited after reading again:
I had read your code again and what it does is you put all your code into the same main condition if(fire) Then you instatiate twice and add force to the object. First on the right and then on the left. As you said it was doing. Once again read what you do and try to understand it.
Apart from that i don't see why it would fire without you touching the button.
Sorry for my mistakes.
I know I have to get along. I appreciate your help. Thank you.
Answer by rOBY GAMES · May 11, 2015 at 04:34 PM
Hello barbe63.
The problem now that fires 10 rounds together when it enters boxcollider2D. 10 shots fired all together, both right and left.
I show you the code:
#pragma strict
var Prefab : Rigidbody2D;
var PrefabSpeed : float = 8;
//var PrefabCreate = Vector2.right;
static var myObject : GameObject;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
}
if (Input.GetKey(KeyCode.D))
{
//move right action
}
if (Input.GetKey(KeyCode.A))
{
//move left action
}
if(Collisions.GRENADE_AMMO <= 0)
{
myObject.SetActive(false);
}
if(!Prefab || !PrefabSpeed)
{
Debug.Log("Il prefab o la velocità non esiste");
}
else
{
//if (Input.GetKey(KeyCode.D))
var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed);
//if (Input.GetKey(KeyCode.A))
PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(-Vector2.right * PrefabSpeed);
Collisions.GRENADE_AMMO --;
GameObject.Find("g_Count").guiText.text = ""+Collisions.GRENADE_AMMO;
print("YOU NOW HAVE " + Collisions.GRENADE_AMMO + " GRENADES");
}
}
//}
HAHAHA!!!
Dude sorry about that laughing but this is quite fun actually.
I want you to understand what you're doing but I'll give you some clues.
First you ask: is fire key is pressed? then if it is you do whatever are inside the brackets which in your code is NOTHING. Then you do the same for D and A nothing to be done if the condition is filled apart those comment i written to you to explain you what you should put into those brackets (not in detail).
Then you do some check on ammo saying something like if ammo is negative set an object inactive.
And then you're doing a prefab check and if both exist you are firing (that's what the else do, it is literraly ATTACHED to the last if) So?
You are sending the firing instantiation permanently as long as both your prefabs are not null. (and twice, one in the left and one in the right)
I bet that's not what you want!
Again try to understand the logic and read what you do.
Now try to tell me: 1) what is the firing action here? 2) where to put it?
Then.
1) Action shot:
if (Input.GetButtonDown ("Fire1"))
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection (Vector2.right PrefabSpeed *);
2) Where to put it?
Now I do not know how to place the shot firing properly. Itself the shooting action shown above is correct, I do not know how to position it. I hope it is correct.
the correct answer was
the action is what you want to do if the button fire is hit so it's :
var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed);
// you may change the Vector2 direction if it's not the direction that you want. I guess it's a platform 2d game so Vector2.right should be nice if the player is facing the right direction
where to put it is inside those brackets:
if (Input.GetButtonDown ("Fire1"))
{
//insert the action here
}
then the next trick you have to do if i'm guessing right and you're doing a 2d platofrm game is something like this:
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
//then you know your player is facing right so set a boolean defined before like this
facingRight=true;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
//this time he's facing left so do this:
facingRight=false;
}
And the total code for this is now:
bool facingRight=true; // set this outside of update but inside the class
void Update()
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
//then you know your player is facing right so set a boolean defined before like this
facingRight=true;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
//this time he's facing left so do this:
facingRight=false;
}
if (Input.GetButtonDown ("Fire1"))
{
if(facingRight) //if you are facing right shot to the right
{
var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(Vector2.right * PrefabSpeed);
}
else //else you are facing left, then shoot to the left
{
var PrefabCreate = Instantiate(Prefab,GameObject.Find("Projectile Spawn").transform.position,Quaternion.identity);
PrefabCreate.rigidbody2D.velocity = transform.TransformDirection(-Vector2.right * PrefabSpeed);
}
}
This code is not including the movement but only the facing direction and the fire instantiation. And the rotation of your bullet wouldn't be good also as you have to set it different if you're facing right or left. (Quaternion.identity is to change)
I won't make the code for you, the purpose here is for you to understand what is in there. Then you could advance on your own.
Hello thank you dui heart for giving me a hand in this script.
$$anonymous$$aybe you're confused but you wrote the script in C #
Look here: bool facingRight = true; and void Update () {}
Your answer
Follow this Question
Related Questions
Player shoot up in the air HELP 1 Answer
GameObject not deactivated. 2 Answers
I cant kill the other player. 1 Answer
Network View mess up colliders? 0 Answers
collider then change rotation converse? 0 Answers