- Home /
Question by
FREAKINGREX · Nov 02, 2019 at 05:27 AM ·
scripting problemdamagenewbiehealth
Having a game object deal damage
I am attempting to have a game object appear from the character when they are moving in a direction and then deal damage to the character when they get close to them. I am very new at this and am trying to start making my own games but am still learning code and everything. Can anyone explain what i am doing wrong and how I can fix it? or even a better way to do it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public GameObject front;
public GameObject back;
public GameObject left;
public GameObject right;
public GameObject player;
public RaycastHit hit;
public int damageDealt = 5;
public PlayerHealth ph;
// Start is called before the first frame update
void Start()
{
}
void FixedUpdate()
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Vector3 bwd = transform.TransformDirection(Vector3.forward * -1);
Vector3 right = transform.TransformDirection(Vector3.right);
Vector3 left = transform.TransformDirection(Vector3.right * -1);
//Activates swords depending on the key press
if (Input.GetKeyDown(KeyCode.W) && Physics.Raycast(transform.position, fwd, out hit, 0.1f) && hit.transform.tag == "Enemy NPC")
{
front.SetActive(true);
damageDealt = ph.TakeDamage;
}
if (Input.GetKeyUp(KeyCode.W))
{
front.SetActive(false);
}
if (Input.GetKeyDown(KeyCode.S))
{
back.SetActive(true);
}
if (Input.GetKeyUp(KeyCode.S))
{
back.SetActive(false);
}
if (Input.GetKeyDown(KeyCode.A))
{
left.SetActive(true);
}
if (Input.GetKeyUp(KeyCode.A))
{
left.SetActive(false);
}
if (Input.GetKeyDown(KeyCode.D))
{
right.SetActive(true);
}
if (Input.GetKeyUp(KeyCode.D))
{
right.SetActive(false);
}
}
}
healthand-damafe.jpg
(86.7 kB)
healthanddamage.jpg
(256.4 kB)
Comment
Your answer
Follow this Question
Related Questions
I need help with my spell dealing damage over time. 1 Answer
Addjustcurrenthealth not working 0 Answers
Health Bar going 0 right away 1 Answer
Having problems with damage script 1 Answer