- Home /
Problem is not reproducible or outdated
First person sword fighting
There doesn't seem to be a step by step tutorial on this can you show me one?
My problem is that I can get my character to hold the sword, but I cant figure out how to make him slash and do damage while still getting the movement controls to work correctly this is the script im using:
var swordPoints : Transform[];
var swingPower : int;
var showGizmosInEditor : boolean = false;
function Update ()
{
var hit : RaycastHit;
for(i = 0; i < swordPoints.length; i++)
{
if(Physics.Linecast(transform.position, swordPoints[i].position, hit))
{
Hit(swordPoints[i], hit);
}
}
}
function Hit(i : Transform, hit : RaycastHit)
{
if(hit.collider.rigidbody)
{
hit.collider.rigidbody.AddExplosionForce(swingPower, i.transform.position, 1.0, 3.0);
}
if(hit.collider.tag == "Enemy")
{
hit.collider.gameObject.SendMessageUpwards("ApplyDamage", swingPower, SendMessageOptions.DontRequireReceiver);
}
}
function OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
if(showGizmosInEditor)
{
if(swordPoints.length != 0)
{
for(i = 0; i < swordPoints.length; i++)
{
if(swordPoints[i] != null)
{
Gizmos.DrawLine(transform.position, swordPoints[i].position);
}
}
}
}
}
I've reformatted your code a bit to make it more readable (remember to mark code in the text with the 010101-button next time). Unfortunately, that didnt help me understand where your problem is in the code? can you explain it a little more?
I got this script from google my problem is that it doesn't seem to take effect when I try to add it as a component it removes my char motor thank you for helping with the script though
Start with a tutorial. Using random code you've found is just going to give you headaches and won't $$anonymous$$ch you a single thing about the scripting you'll need to actually make games.
I dont think I have removed/changed your code @puzzledragon I merly formatted it for viewing purpose. I agree with @ChrisD - perhaps you should think of starting with something a little easier and then work your way up through tutorials. Scripting is like writing a book, you need to learn your ABC first.
Don't do it through scripting do it through animation
Answer by JUnityer · Jul 21, 2011 at 11:38 AM
Agree with the commenters, start by learning analyzing code, and don't just pick other's scripts from the internet and expect that they work immediately! :)
But I'm going to give you a little help anyway:
Make a script for your sword or add stuff to your movement script. Check if the user presses a button (mouse1 for example), and then make the sword swing using an animation. Then check if there is collision between the sword and the object you want to kill.
Search the scripting reference for all these functions.
Follow this Question
Related Questions
Swinging a sword through code 3 Answers
Best way to detect sword hit 0 Answers
RPG Swing attack hit detection 6 Answers
on collision damage 1 Answer
What is the best way to do third person melee combat? 1 Answer