- Home /
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.
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; }
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!
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.
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.
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.
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613032336im_/https://answers.unity.com/themes/thub/images/avi.jpg)