Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DavidD · Aug 05, 2012 at 10:45 PM · meleeswordswing

Swinging a sword through code

Hey there!

I'm new to unity, though I'm not new to coding so no worries about that.

Now, what I want to do is to swing my sword through code instead of using animations since I'm horrible at rigging, animating and all that. The only part I need help with is the actual swing.

First I thought I'd parent the object to my player (which is just a cube). Now, how would I swing the sword through a C# script which is attached to the player (the cube)? No need to get super-fancy right now. All I really need to know is how to actually move the sword in the first place.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RodrigoSeVeN · Aug 06, 2012 at 12:24 AM 0
Share

If you are familiar with transforms and it's properties, you just need to create a transform variable on your script and assign the sword there. From there, you will code your swing. The reason an animation is used is because the movement is normally repeated over and over. You will just end up doing this movement manually with coding.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TyrusPeace · Aug 06, 2012 at 02:01 AM

Just moving the thing would generally involve calling lerp or slerp on the angle you want your sword to be at when the swing is over and updating it per update:

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Slerp.html

Slerp could work nicely because you can interpolate it across the surface of a sphere, but personally I'd just use lerp to change the angle of the thing over time in Update() after swinging and set it via eulerAngles:

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html http://docs.unity3d.com/Documentation/ScriptReference/Transform-eulerAngles.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DavidD · Aug 06, 2012 at 10:48 AM 1
Share

Well, now I feel dumb because I was so simple. I thought I had to do a bunch of things but nope. Simple enough. I just changed it around and used transform.localPosition ins$$anonymous$$d! Cheers!

avatar image
4

Answer by DavidD · Aug 06, 2012 at 11:03 AM

In case anyone else wants the basic code:

 swordTransform = transform.FindChild("Sword");

 void Update() {
 swordTransform.transform.localPosition = Vector3.Slerp(swordTransform.localPosition, new Vector3(1, 0, 1), 0.01f);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Alpha_Guac · Oct 19, 2015 at 10:36 AM

I found that using Vector3.Lerp was more what I wanted than Slerp. I would suggest switching between the two to find what best works for what you want

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i make a weapon swing like a crowbar or a baseball bat 2 Answers

on collision damage 1 Answer

Swinging a sword without an animation... 1 Answer

RPG Swing attack hit detection 6 Answers

What is the best way to do third person melee combat? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges