- Home /
Instantiate after setting values?
Hi im trying to create a topdown zelda style game. im currently working on basic shooting mechanics. eventually the player will only be able to move in 4 directions
i am trying to get the shooting system to work this way. i have a way to know which way the player is facing but i cant really use it as the object is already Instantiated before i can set those values.
is there a way i can set the values first or delay the Instantiatetion ?
here is my current code. currently the bullet hits the player itself before it has a chance to move out. i like to keep self damage aswell
  [Command]
     void CmdFire()
     {
         // create the bullet object from the bullet prefab
         var pos = transform.position;
         var bullet = (GameObject)Instantiate(
             bulletPrefab,
             pos - transform.forward,
             Quaternion.identity);
 
         float inputX = Input.GetAxis("Horizontal");
         float inputY = Input.GetAxis("Vertical");
         //Taking the input And figuring out what direction were facing
         if (inputX > 0.2 || inputX < -0.2 || inputY > 0.2 || inputY < -0.2)
         {
             //anim.SetBool("Walking", true);
             if (inputX > 0)
             {
                 //anim.SetFloat("LastMoveX", 1f);
                 pos.x += 1;
                 bullet.GetComponent<Rigidbody2D>().AddForce(transform.right * 200);
             }
             else if (inputX < 0)
             {
                 //anim.SetFloat("LastMoveX", -1f);
                 pos.x -= 1;
                 bullet.GetComponent<Rigidbody2D>().AddForce(-transform.right * 200);
             }
             else
             {
                 //anim.SetFloat("LastMoveX", 0f);
                 pos.x = 0;
                 pos.y -= 1;
                 bullet.GetComponent<Rigidbody2D>().AddForce(-transform.up * 200);
             }
 
             if (inputY > 0)
             {
                 //  anim.SetFloat("LastMoveY", 1f);
                 pos.y += 1;
                 bullet.GetComponent<Rigidbody2D>().AddForce(transform.up * 200);
             }
             else if (inputY < 0)
             {
                 // anim.SetFloat("LastMoveY", -1f);
                 pos.x = 0;
                 pos.y -= 1;
                 bullet.GetComponent<Rigidbody2D>().AddForce(-transform.up * 200);
             }
             else
             {
                 //anim.SetFloat("LastMoveY", 0f);
                 pos.x = 0;
                 pos.y -= 1;
                 bullet.GetComponent<Rigidbody2D>().AddForce(-transform.up * 200);
             }
         }
         else
         {
             //anim.SetBool("Walking", false);
             pos.x = 0;
             pos.y -= 1;
             bullet.GetComponent<Rigidbody2D>().AddForce(-transform.up * 200);
         }
         NetworkServer.Spawn(bullet);
 
         // make bullet disappear after 2 seconds
         Destroy(bullet, 2.0f);
     }
Just had a thought. i could have the direction checking separate and have a game object move to different position depending on the way im facing and use that as a position when iInstantiate
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                