So I have been working on a script for my game at school but cant seen to be able to create a damage script. My game is using the unity’s built in networking and coding in c#. I’m using ray-cast and just need help with the damage part of it.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Shooting : NetworkBehaviour {
public float minWeaponDamage = 25f;
public float maxWeaponDamage = 50f;
public float weaponRange = 100f;
public bool debugMode = true;
public Camera FPSCamera;
private void Update()
{
Ray ray = FPSCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hitInfo;
if(debugMode == true)
{
Debug.DrawRay(ray.origin, ray.direction * weaponRange, Color.green);
}
if (Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(ray, out hitInfo, weaponRange))
{
if(hitInfo.collider.tag == "Players")
{
//NEED DAMAGE CODE HERE
}
}
}
}
}
I can suggest you to check official unity tanks tutorial. The math for the damage is the same in your scenario In case you want deal full damage at zero distance and min damage at max weapon range.
damage code:
//NEED DAMAGE CODE HERE
float distanceToTarget = (hitInfo.collider.transform.position - FPSCamera.transform.positon).magnitude;
float damageRange = maxWeaponDamage - minWeaponDamage;
float ratio = 1f - distanceToTarget / weaponRange;
ratio = Mathf.Max(0f, ratio); // don't allow negative ratio as your target would gain health ;).
//ratio will be 0 at weapon range (distance 100) and 1 at zero distance.
var damage = ratio * damageRange + minWeaponDamage;
There might be some mistakes as I wrote the code from scratch in here witchout checking.
the only thing i dont quite know what to do is how to apply the damage var then. i would have to make a maxhealth public float and then apply it to have it be maxhealth - damage. (I’m a little new to c# sorry)
don’t try to do the health change in the shooting script.
Make a script attached to the thing being hit, have that handle the health and have it expose a function that takes the damage as a parameter. The shooting script then just retrieves the health component from the thing being hit and calls that function with the damage value as the parameter being passed.
rather than nitpicking the typos in help you’re getting you might want to look at the tutorial you’re being pointed towards…
Also, I’ll point out something which is not directly related to your question but… =>
Try to avoid public fields in case you don’t really need them. It may save you some headaches later as it’s better approach to encapsulate everything and expose (to public) only properties and methods which are really necessary to be used from external class.
if you need to have fields visible in inspector but not to access it from another class, just use [SerializeField] attribute.
e.g
For example the method (e.g. TakeDamage) @LeftyRighty mentioned needs to be public as you really need to access it externally. But not field you need to access in Unity Inspector only.
Today I learned something new and I really like it so I needed to put it here to make the thread complete ;).
Haven’t noticed before Lerp and Inverse lerp can pretty easily solve that in that elegant way.
//NEED DAMAGE CODE HERE
float distanceToTarget = (hitInfo.collider.transform.position - FPSCamera.transform.positon).magnitude;
var distanceRatio = Mathf.InverseLerp(0f, weaponRange, distanceToTarget); //this will translate distanceToTarget to number between 0 and 1 inclusive (0 when distanceToTarget is 0, 1 when distanceToTarget is weaponRange)
var damage = Mathf.Lerp(maxWeaponDamage, minWeaponDamage, distanceRatio); //this will give you 50 when distanceRatio is 0 and 25 when distanceRatio is 1