Be the first to post for 'Player Melee Damage'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

Went about this in a slightly different way, so thought I’d share my code.

I’m not a fan of early return statements, so avoid them whenever possible. In this case it made more sense to me to simply make the if statement read “if it’s time to attack and I’m in range.”

void OnMouseClicked (RaycastHit raycastHit, int layerHit) {
	if (layerHit == enemyLayer) {
		GameObject enemy = raycastHit.collider.gameObject;
		float enemyDistance = Vector3.Distance(transform.position, enemy.transform.position);
		currentTarget = enemy;
		if(Time.time - lastHitTime > hitrate && enemyDistance < attackRange) {
		 	currentTarget.GetComponent<Enemy>().TakeDamage(damagePerHit);
		 	lastHitTime = Time.time;
		 }
	}
}
1 Like

My console says:
get_time is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour ‘Player’ on game object ‘Player’.
See “Script Serialization” page in the Unity Manual for further details.
UnityEngine.Time:get_time()
DragonRPGTutorial.Player:.ctor() (at Assets/_DragonRPG/Player/Player.cs:18)
UnityException: get_time is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour ‘Player’ on game object ‘Player’.
See “Script Serialization” page in the Unity Manual for further details.
DragonRPGTutorial.Player…ctor () (at Assets/_DragonRPG/Player/Player.cs:18)

might just minus attack on attack and add attack after 0.5 delta time is attack > 1. or… idk. Not sure why I have htis error and yours doesn’t

I really should stop marathon programming. I was trying to initialize it Time.Time, so…

I think I need some help with this. The character I had imported doesn’t appear to have anywhere to attach the weapons to.

My player has ‘Hands’ listed - but with only one central transform - so no good.

It also has some ‘Reference’ transforms. - but they don’t move with the hands - they are childed directly to the top level player transform - so only move with the player, but not with the hads…:

But the skeleton shows more detail and a right-hand can clearly be seen…:

I’m sure I have done something wrong - but I did struggle to know which assets I should be using to get the player animations working initially, and it was a bit of trial and error.

Can anybody help?

Never mind - it was through Challenger Reference - to hips - to spine - to shoulders etc…

its that old song… Your hip-bone’s connected to your back-bone. you’re back-bone’s connected to your… etc… :slight_smile:

Using the LookAt method, using aimOffset is a bit difficult. Instead, I just had the projectile look at an empty transform that I put on the player’s head and then multiplied its resulting forward vector by the velocity.

Also, did anyone else have a weird issue where initializing the aimOffset vector in the constructor made the value default to 0,1,0, even after experimenting and setting it lower to 0,0.1,0? It didn’t happen anymore when I initialized it in ththe constructor. Not that it matters since I didn’t end up using it.

,
void OnMouseClick(RaycastHit raycastHit, int layerHit){
if(layerHit==enemyLayer){
var enemy = raycastHit.collider.gameObject;
currentTarget = enemy;
Component damagableComponent = enemy.GetComponent(typeof(IDamageable));
if (damagableComponent&&Input.GetMouseButtonDown(0))
{

			(damagableComponent as IDamageable).TakeDamage(1);//damage=damageCaused
		}

}
}

AND IN PROJECTİLE I CHANGED;
,
void OnTriggerEnter(Collider col)
{

    Component damagableComponent = col.gameObject.GetComponent(typeof(IDamageable));
   // print("damagableComponent" + damagableComponent);
	if (col.gameObject.tag=="Player"&&damagableComponent)
    {

        (damagableComponent as IDamageable).TakeDamage(damageCaused);//damage=damageCaused
    }
}

Yeah freshplay2, that’s one way to limit the amount of attacks per second, just leaving it to the speed the player can press the button. Though you’ll need to limit it later when you implement animations.

Tags and layers are nice for “filtering” game objects, since you can access to them no matter the component.

===

I did something for instantiating stuff. I know I’ll be adding inventory thing so I just did this for myself.

So yeah, yay! (I’ll remove that extra sword later… or leave it there, and just give a cheesy excuse).

Privacy & Terms