- Home /
how to disable instantiating projectile for the moment when the player jumps and hits the space bar to fire?
hii . i have a 2d game . and have a player who can jump whith up arrow and fire with space bar . now i want to disable the space bar when i jump , so that if i hit the space when i jump it wouldnt work and dont shoot any projectile for a while . the reason that i want to do this is that now if i hit the up arrow and jump and while jumping hit the space bar to fire it fires projectile but jumps again while its in the air and goes a lil upper!!! tnx
the reason that i want to do this is that now if i hit the up arrow and jump and while jumping hit the space bar to fire it fires projectile but jumps again while its in the air and goes a lil upper
i think it can be fixed and it will be more correct solution then just disabling firing.
anyway, without scripts sources we can't help you. post a script where you handle jumping and firing.
Answer by shahinexir · Aug 13, 2012 at 06:22 PM
here is my player script . really apreciate if u can help me :
var playerSpeed : int;
var jumpHeight : int;
var fireBallPrefab : Rigidbody;
function Update () {
//1- player movement
amtToMove = playerSpeed * Input.GetAxisRaw("Horizontal") * Time.deltaTime; // amount to move the player
transform.Translate(Vector3.right * amtToMove);
//------------------------------------------------------------------------
//2- player rotation...!
if (Input.GetKey("left") || Input.GetKey("a"))
{
transform.rotation = Quaternion.Euler(0,180,0);
}
if (Input.GetKey("right") || Input.GetKey("d"))
{
transform.rotation = Quaternion.Euler(0,0,0);
}
//--------------------------------------------------------------------------
//3- player jump
// player can countinusly jump when you hold the up button. if its transform position in y axis is equal to 0.73(on the ground)
if ( (Input.GetKey("up") || Input.GetKey("w") ) && transform.position.y == 0.73)
{
amtToJump = jumpHeight + Time.smoothDeltaTime ;
transform.Translate(Vector3.up * amtToJump );
}
//4- instantiate fireBall
if (Input.GetKeyDown("space"))
{
var tempfireBall : Rigidbody;
tempfireBall = Instantiate(fireBallPrefab,Vector3(transform.position.x , transform.position.y, transform.position.z),transform.rotation);
}
}
very strange script 8)
in this script you can jump only if your position is in height 0.73. so. if you are jumping already, you are not able to jump again. also, in "space" button nothing can let player jump again. something here is not explained still.
thats right.i want to make a gameplay like super mario . can u understand what i mean? i want that kindda jump. have u any idea about jumping? what should i do? :( also about rotation there is a problem that if i want to move to left the player rotates to the left but walks to right direction!!! donno why!!! really need some edit
in supermario player can jump not only from ground, but from other object that has other height. so your script is incorrect a little. also, how in your case player is falling down? is it rigidbody with gravity ot smthing else?
by default transform.Translate moves object in local space, but you use it like in world space. so:
or move object always in one direction in local space and rotate separately
or move object in world space
transform.Translate(Vector3.right * amtTo$$anonymous$$ove, Space.World);
yes , i have rigid body with gravity on my player . and also tnx alot for your help.its now rotating and moving well ;) about jump height , i know what youre saying and your right but there is a problem . if i remove the limit 0.73 then if i jump there wont be any limit on jump height ony axis , and the player will fly to the sky and come back again after a few seconds by gravity force!! . so what do u think? do u have any idea how to fix it ? tnx . and yes its a lil wierd script :D but im new . so really apreciate if u tell me wich parts are wrong or look strange . tnx
once you use a rigidbody with gravity, to jump able you can just check if rigidbody velocity in Y axis is close to zero:
if ( (Input.Get$$anonymous$$ey("up") || Input.Get$$anonymous$$ey("w") ) && $$anonymous$$athf.Abs(rigidbody.velocity.y) < 0.01)
and, to jump simply add vertical speed:
rigidbody.velocity.y = 5.0;
Answer by subcod · Aug 13, 2012 at 06:22 PM
1.make a raycast under the player
var ableToJump:boolean; If(!(Physics.Raycast(transform.position,Vector3.down,distance))) {
//Everything in here executed when the character has jumped so high the raycast doesn't //touch the ground
ableToJump=False; } if(ableToJump&&Input.getButton("Jump"))
{ transform.position.z+=5; }
would u pls explain me about the first if ( condition ) part? what is actually happening there ? and what is that ({ transform.position.z+=5; } ) doing? and why z axis?! im making a 2d game tnx .
sorry im late, The first if condition is to check if the player is on the terrain or object.
transform.position.z+=5 is the same as transform.position.z=transform.position.z+5
z is the direction of the transform
Your answer
Follow this Question
Related Questions
Third Person Shooting Script Stopped Working 1 Answer
Destorying prefab and instanstiating again? 0 Answers
Checking if object intersects? 1 Answer
Enemy Damage problem 1 Answer
how do i stop automatic firing? 3 Answers