Question by
joshuakeedy · Apr 09, 2019 at 05:25 PM ·
c#movement2d gameeditor-scriptingshooting
(Help) I am having trouble with creating a simple 2D game in Unity. Please help. :)
Hello all, I have just started using Unity and C# and I have been trying to create a 2D game however I am having trouble making the player shoot in the direction that they are walking and I am hoping someonw could help me with this. In addition I was hoping to add the ability to press a button and be able to shoot upwards as well. Many Thanks, Josh. The scripts : PlayerController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
float move = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(speed * move, rb.velocity.y);
}
}
Weapon:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
//shooting logic
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
}
}
Bullet:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bullet : MonoBehaviour
{
public float speed = 20f;
public int damage = 40;
public Rigidbody2D rb;
public GameObject impactEffect;
// Use this for initialization
void Start()
{
rb.velocity = transform.right * speed;
}
void OnTriggerEnter2D(Collider2D hitInfo)
{
Health enemy = hitInfo.GetComponent<Health>();
if (enemy != null)
{
enemy.TakeDamage(damage);
if (damage == 100)
{
Destroy(gameObject);
}
}
Instantiate(impactEffect, transform.position, transform.rotation);
//Destroy(gameObject);
}
}
Comment
Your answer
Follow this Question
Related Questions
Why won't my 2D Sprite Move? 1 Answer
Shooting bullets in mouse direction for 2D game 1 Answer
I can't make my character move 0 Answers
How to move a game object to a position after selecting it 0 Answers
2D movement along vector 1 Answer