- Home /
Too subjective and argumentative
Need A Combat System.
Hello, I have been trying to get a working combat system for my RPG for a while. I am new to unity and I cannot seem to get one working. I have decided to use SendMessage for the combat and I am really stumped. I have found some scripts on the internet that I am editing but I cannot get them to work the way I want them to. I need this script below to send a message to the enemies target that says damage where it says //attack if close enough. The script below is used by enemies.
// Chasing And Attacking
var target : Transform; //the enemy's target
var moveSpeed = 3; //How fast the enemy is
var rotationSpeed = 3; // How fast enemy turns
var attackThreshold = 3; // Attacking Range
var chaseThreshold = 10; // Enemys sight range
var giveUpThreshold = 20; // Distance when enemy gets tired
var attackRepeatTime = 2000; // How fast the enemys attacks come
var reciever : GameObject;
private var chasing = false;
private var attackTime = Time.time;
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
reciever = GameObject.FindWithTag("Player").gameObject; //target the player
}
function Update () {
// check distance to target every frame:
var distance = (target.position - myTransform.position).magnitude;
if (chasing) {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
// give up, if too far away from target:
if (distance > giveUpThreshold) {
chasing = false;
}
// attack, if close enough, and if time is OK:
if (distance < attackThreshold && Time.time > attackTime) {
// Attack! (call whatever attack function you like here)
attackTime = Time.time + attackRepeatTime;
// Replace 5 with whatever damage that enemy deals here
reciever.gameObject.SendMessage ("Damage", 5.0);
}
Here is the script that is used by the player. I need it to - the players health by the float that came with the message every time its repeated.
var health = 100;
function Damage(Damage : float){
health -(Damage);
}
if(health <- 0){
print("Game Over");
health = 0;
Destroy (gameObject);
}
This game will eventually be in multiplayer. If you can fix this then you are truly awesome.
If this game will be multiplayer, may I give you a heads up, that porting from local to network is a shit ton of work, if you han't started with networking in $$anonymous$$d... Holing information about the game state in many different objects, is the enemy of networking!! (just a heads up :) )
So what exactly isn't working the way you want? Simply saying "it doesn't work" won't attract many answers.