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 Leocesar · Nov 04, 2011 at 02:57 PM · camerarotationfirst personsword

Sword slash (First Person POV camera rotation)

I am creating a First Person medieval type game and would be great if the player camera rotates when he slashes the sword. I already have the arms and sword animation, but without the “head movement” the slash seems dull and unrealistic. When a swordsman releases a sword strike his head rotates along with the sword. That´s the effect I want to incorporate. It resembles the head movement when gun recoil pushes the shooter body backwards but instead going backwards, the camera should “follow along the sword movement / animation (only rotation). Thanks in advance.

Comment
Add comment
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

2 Replies

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

Answer by Leocesar · Nov 23, 2011 at 05:10 PM

Based on the code found in this thread (http://forum.unity3d.com/threads/70271-Simple-weapon-recoil-script) I managed to get the effect I was aiming for. Being: when you "fire" your sword animation, the camera moves with the sword (see Skyrim sword dynamics to see what I mean).

Here is the answer (it is nothing different from gun recoil, but applied to a greater degree in the sword context):

Create an empty game object and parent it to your character controller, then parent your FPS camera to the empty game object and finally attach the script bellow to your empty game object (recoil controller).

using UnityEngine; using System.Collections;

public class Recoil : MonoBehaviour { private float recoil = 0.0f; private float maxRecoil_x = -20f; private float maxRecoil_y = 20f; private float recoilSpeed = 2f;

 public void StartRecoil (float recoilParam, float maxRecoil_xParam, float recoilSpeedParam)
 {
     // in seconds
     recoil = recoilParam;
     maxRecoil_x = maxRecoil_xParam;
     recoilSpeed = recoilSpeedParam;
     maxRecoil_y = Random.Range(-maxRecoil_xParam, maxRecoil_xParam);
 }

 void recoiling ()
 {
     if (recoil > 0f) {
         Quaternion maxRecoil = Quaternion.Euler (maxRecoil_x, maxRecoil_y, 0f);
         // Dampen towards the target rotation
         transform.localRotation = Quaternion.Slerp (transform.localRotation, maxRecoil, Time.deltaTime * recoilSpeed);
         recoil -= Time.deltaTime;
     } else {
         recoil = 0f;
         // Dampen towards the target rotation
         transform.localRotation = Quaternion.Slerp (transform.localRotation, Quaternion.identity, Time.deltaTime * recoilSpeed / 2);
     }
 }

 // Update is called once per frame
 void Update ()
 {
     recoiling ();
 }

}

From your gun script when a shot is triggered call the Recoil script this way:

void Start () { cam = GameObject.FindWithTag ("MainCamera").transform; recoilComponent = cam.parent.GetComponent<Recoil>(); }

IEnumerator Fire () { recoilComponent.StartRecoil(0.2f, 10f, 10f); // other firing code; }

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
1

Answer by syclamoth · Nov 04, 2011 at 03:01 PM

I think that'd look really weird, personally. Make sure its subtle, and doesn't rotate the camera at all- just a bob down in the direction of the swing.

I'd make it part of the animation. So, along with the bones in the arm and the weapon, have a bone for the camera which gets animated along with the rest- then just attach your camera to that bone!

Comment
Add comment · Show 7 · 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 Leocesar · Nov 04, 2011 at 10:42 PM 0
Share

Yes it must be subtle, otherwise people will puke while playing my game (not the desired response). I was ai$$anonymous$$g for a procedural solution, but what you suggested seems interesting, attach the player camera to the arm rig, if well done, can be less messy then try to do this by code. Thanks. But I would like to see some code to get the same effect because all my camera movement is defined in scripts and not in animation blending.

avatar image syclamoth · Nov 04, 2011 at 11:09 PM 0
Share

It's hard to get a nice-looking effect set up in code, since it's much less intuitive than using straight animations. What you would do there, is still do all the rest of your camera movements with scripting, and only use the animated bit in specific cases. If you make the parent object the one with all the camera movement scripts on it, there's no reason why you can't do both.

avatar image Leocesar · Nov 04, 2011 at 11:53 PM 0
Share

I agree with you completely. I'll have to rework my arm rig a bit, but I'm confident that your proposal will solve my problem. When I have the results I'll let you know. Thanks.

avatar image Leocesar · Nov 06, 2011 at 01:29 AM 0
Share

The bone parent solution won´t work in my case because I switch arms for each weapon. It would require a parenting workaround nightmare. If I were using only one imported bone rig it would be nice, but the way I choose to set the weapons it won´t work at all. I am still pursuing a procedural solution.

avatar image syclamoth · Nov 06, 2011 at 11:50 AM 0
Share

Well, if you have one bone in each rig which you designate as the 'camera bone' and then change between them in script, it's still easier than doing it all procedurally.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Best way to rotate child and parent game objects in different axes without the rotation going crazy or glitchy? 1 Answer

First Person Camera Rotation Help 1 Answer

First Person Camera not being child of the character/ Set child's rotation to global rotation 2 Answers

Camera, Look rotation, smoothly? 0 Answers

Camera not rotate when following player 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