Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » FREE Project Downloads & Recources » Scripts » Creating a 2D game and having trouble with the animaton or the hand movement and the projectile being fired part.

Creating a 2D game and having trouble with the animaton or the hand movement and the projectile being fired part.

View previous topic View next topic Go down  Message [Page 1 of 1]

eddie21


Member [Lv. 1]
Member [Lv. 1]
Hi all,
I'm currently a bit stuck and could use some help.
I'm currently creating a 2D game and having trouble with the animaton or the hand movement and the projectile being fired part.
I want to add a delay to my projectile so after searching around tutorials they have stated to use StartCoroutine. I've tried other methods but cant seem to get the delay in between the animation movement to the rocket being fired out.
please can any one help?
thanks in advance for those who do help.

void Start () {
myRB = GetComponent<Rigidbody2D> ();  // grab and store physics
myAnim = GetComponent<Animator> (); //grab and store sprite anim
facingRight = true; //Set up to face fight at the start of the game

}

//update once per frame
void Update (){
if (grounded && Input.GetAxis ("Jump") > 0) {
grounded = false;
myAnim.SetBool ("Isgrounded", false);
myRB.AddForce (new Vector2 (0, jumpHeight));
}

//player shooting
if  (Input.GetAxisRaw ("Fire1") > 0)

{
myAnim.SetTrigger("shooting");
StartCoroutine ("waitThreeSeconds");
fireRocket() ;

}

}

IEnumerator waitThreeSeconds()
{
yield return new WaitForSeconds (5);
}


// Update is called once per frame
void FixedUpdate () {

// check if we are grounded if no then we are falling
grounded = Physics2D.OverlapCircle(groundCheck.position, groundcheckRadius, groundLayer);
myAnim.SetBool ("Isgrounded", grounded);
myAnim.SetFloat ("verticalSpeed", myRB.velocity.y);

float move = Input.GetAxis ("Horizontal");  //sets the keyboard movement
myAnim.SetFloat("Speed",Mathf.Abs(move)); //switching animation idle - running

myRB.velocity = new Vector2 (move*MaxSpeed, myRB.velocity.y); //moving forward and back

if (move > 0 && !facingRight) {  
flip (); //fto faceright direction whilst moving

} else if (move < 0 && facingRight) {
flip ();
}

}

void flip(){  // flips the graphics
facingRight =!facingRight;
Vector3 theScale = transform.localScale; //takes the chars scale
theScale.x *= -1; //sets the direction facing
transform.localScale = theScale; //places it back into the char
}



void fireRocket(){
if (Time.time > nextFire) {


nextFire = Time.time + fireRate;
if (facingRight) {
Instantiate (bullet, gunTip.position, Quaternion.Euler (new Vector3 (0, 0, 0)));
} else if (!facingRight) {
Instantiate (bullet, gunTip.position, Quaternion.Euler (new Vector3 (0, 0, 180f)));
}
}
}

}

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum