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
Share
Go down
Member [Lv. 1]
Member [Lv. 1]
Posts : 2
Points : 428
Rep : 0
Join date : 2017-05-26
View user profile

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

on Fri May 26, 2017 6:34 am
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)));
}
}
}

}
Back to top
Permissions in this forum:
You cannot reply to topics in this forum