Question by
Starrial · May 16, 2018 at 05:42 PM ·
shootinganglemouseposition
Unity 2D Shoot towards mouse position
Hey! I have been using Unity for only a couple of months now and iv'e been working on a pixel dungeon type game. I'm having quite the trouble to make my bullet shoot at any angle. Right now the only way my bullet can travel is up. I've tried many different solutions but none of them works. Is there anyone who have a solution? Here's my script:
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour { public GameObject PlayerBulletGO;//this is our player's bullet prefab public GameObject BulletPosition01;
public float moveSpeed;
public GameObject particle;
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
//fire bullets when the left mouse is pressed
if (Input.GetButtonDown ("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray))
Instantiate(particle, transform.position, transform.rotation);
//instantiate the first bullet
GameObject bullet01 = (GameObject)Instantiate (PlayerBulletGO);
bullet01.transform.position = BulletPosition01.transform.position;//set the bullet initial position
}
if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw ("Horizontal") < -0.5f) {
transform.Translate (new Vector3 (Input.GetAxisRaw ("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
}
if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw ("Vertical") < -0.5f) {
transform.Translate (new Vector3 (0f, Input.GetAxisRaw ("Vertical") * moveSpeed * Time.deltaTime, 0f));
}
anim.SetFloat ("MoveX", Input.GetAxisRaw ("Horizontal"));
anim.SetFloat ("MoveY", Input.GetAxisRaw ("Vertical"));
}
}
Comment
Your answer
Follow this Question
Related Questions
Shoot towards mouse with offset 0 Answers