raycasting

alright so I’m having a slight problem. I’m trying to get back into making video games, and so far it’s been going well. Things are coming back to me as easily as riding a bike, however, I’m having a problem with my combat system.

basically, I’m wanting a simple combat system for now, the game is 3D and I’m just trying to get a basic sword swing and shield block system going. However, I’m stuck on the sword swinging part, more specifically, registering the sword hitting another game object and using script to reduce the npc’s health. At first I was trying the old way with using colliders, however it got very messy very quickly, so instead I’ve opted to go the raycast route. At least, I hope that’s the correct route.

I need to have a script that is called when I press say, spacebar. Right now I can verify that the function is being called, however when I use the raycast I can’t get a debug line from it.

void TempAttack(){
		Debug.Log ("Function Called");
		RaycastHit hit;
		Vector3 fwd = transform.TransformDirection (Vector3.forward);
		if (Physics.Raycast (transform.position, fwd, 10)) {
			Debug.Log ("Raycast Hit");
			}

	}

any help would be greatly appreciated. I also need to obtain information about the hit target, and if possible call a function on that target’s “Enemy.cs” script.

i dont see why that would not work if there is a ridgidbody (or just a collider) on the object you are hitting and it is less than your ray distance

if hit success you can do

var eScript = collided.gameObject.GetComponent(“Enemy”) as Enemy;
blah = eScript.blah;
or
eScript.blah = blah;

Won’t work without a valid collider. I’m not sure a character controller is a valid collider.

You might want to call Debug.DrawLine to draw a line that matches your raycast line, just to see if it is actually is hitting your collider. You should see the debug line in the Scene View.