Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Unity 2d Fire Bullet In Facing Direction

$
0
0
Hello all kinda new to unity 2d so i've had a bit of trouble getting use to it, making a top down 2D platform game. So far got the player firing a bullet along the Z-Axis, and the bullet being destroyed. My question is **i have force being applied on the x-axis**, **is this correct?** And is their a better way of coding this attack script, kinda **wanting to use the direction the character is facing to signal to the fire script which direction to fire in**. How would i do this? My script so far is -i also have a very simple deletion script (was getting annoyed at all the instantiated bullets) that deletes the bullet once it hits anything tagged as enemy. using UnityEngine; using System.Collections; using System.Collections.Generic; public class FireScript : MonoBehaviour { //the object that will be spawned public GameObject bulletPrefab; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetButtonDown("Fire1")){//when the left mouse button is clicked print ("1");//print a message to act as a debug FireBullet();//look for and use the fire bullet operation } } public void FireBullet(){ //Clone of the bullet GameObject Clone; //spawning the bullet at position Clone = (Instantiate(bulletPrefab, transform.position,transform.rotation)) as GameObject; Debug.Log ("Bullet is found"); //add force to the spawned objected Clone.rigidbody.AddForce(1000,0,0); Debug.Log ("Force is added"); } }

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>