We are having a problem with our first person camera and the character animations for our game. A lot of the animations move, (sword swinging attacks, etc.) rather than playing in place, and the camera will not follow the character during these animations. The characters are going through the camera and causing visual/gameplay problems. Does anybody know of a script that allows the camera to follow the character during these animations that move? I really appreciate any help, thank you.
Our game is a first person, fantasy arena pvp game
constrain the camera only to the head joint’s position, not rotation.
during an animation you dont want the camera to tilt, try updating the world space rotation to 0 while the animation plays and then stop when the animation finishes.
So then create and empty Game Object, Parent it to a player (NOTE: Place it in a same position with a HeadBone "Just copy transform properties). Then find a head bone and parent it to that GameObject you just created, do the same thing with a MainCamera. Its should look like
Okay if you have it on the head bone’s position alone and not the rotation, it won’t rotate along with several of the animations.
I’ve come up with a solution to this via coding to parent it while maintaining the X and Z rotations to a locked position to maintain stability, letting the Y position rotate by the EulerAngles. The only problem is that the camera rotates to the right slightly every time an animation is played, eventually doing a complete 180.
I think it’s the alignment code, but I can’t figure out why:
function Update()
{
transform.rotation = Quaternion.Euler(lockPos, transform.rotation.eulerAngles.y, lockPos);
}
For a solution, I thought about having the first position it is in memorized and then recalled after the animation is played, but I can’t quite put it into code right now, thinking there is a better solution or a fault in my code already.
Does anyone have a suggestion?
Edit: Terminal can you give me details about what you meant on the World space rotation?
if(Special){
// make it a child of the current object
cameraTransform.parent = transform;
cameraTransform.rotation = Quaternion.Euler(0, cameraTransform.parent.eulerAngles.y, 0);
// place it at a certain position
cameraTransform.localPosition = Vector3(-0.045, 0.18, -0.036);
}
else{
cameraTransform.parent = Mainbody;
}
The only problem is that once I change the position it won’t go back after the special is done.
Don’t parent it. Parenting will cause the child to inherit the parents coordinate space (local space), and will therefore follow the parent in scale, rotations and position. Then you would be forced to counter-animate the rotations to undo the parent stuff. So, the solution would be to keep your camera in world-space (or a camera hierarchy, depending on your normal setup). Then just set the position of the camera to the position of the target (position is a worldspace calculation). Now the rotation will not be influenced by the head, only the position. E.g.
cameraControl.position = headJoint.positionThat is it (just change the objects you are setting to whatever makes sense).
If you want, you can grab our free asset package UnityConstraints (http://Constraints.path-o-logical.com). The code is so simple (as demonstrated above), but if you prefer to keep it modular, you can drop on one of our Transform constraints, set it to position only and drop the head joint on the target slot. This will constrain the object to the head joint in position only. (I’ll add some videos in the next few days.)
These constraints have a robust framework so you could turn them on and off as well. (When you turn it off, it will just become free to be moved anyway you want again, so you could translate it back to 0, for example.) I haven’t added a version which will blend positions over time, but I could if you need. I planned to do this eventually anyway.
How is it possible to have a head joint without a position?
Everything is, it is just a matter of describing a specific behavior and then adapting the code or setup or both to make it work
Tilt = rotation = bad?
Rotate = good?
Can you please start over and explain exactly what you are after. In my mind I thought you wanted a follow camera that also moves forward with the character as it plays action animations in a sympathetic way (not exact, which would probably make people sea-sick, but in a smooth simple way). Is this pure first person? Meaning you see nothing, or just arms, or 3rd person where you see a whole body? It sort of sounds like both. As you can tell, I’m confused
Okay.
The game is in first person view. The camera is part of the prefab of the character and positioned in front of the character. However, because of the animations, the character would appear in front or away from the camera, breaking first person view.
To fix this, I first attempted parenting the camera to the head joint, which worked with the exception of tilting on the Z and X axis. To correct this I added a script that locked the Z and X axis and that worked, except for the fact that the camera kept drifting and turning to the side by the Y axis.
So to fix that, I changed the method of having the camera parent itself to the head joint during certain animations, and other wise return to being a child of the prefab as a whole.That brought up the current problem.
So you DO have a head joint, or else you wouldn’t have been able to parent anything to it… So…
Parent your camera to the prefab (the same as no parent/world-space. I assume your prefab node itself doesn’t move or rotate and all functionality is on children)
Then in each frame you would…
rotate the camera based on user input
Find the position of the head joint
set the camera to the position of the head joint
You may have to do this in LateUpdate(), or even better, in a co-routine with WaitForEndOfFrame() to be sure you aren’t fighting anything else going on and the animation has a chance to update before you start referencing positions. All of the UnityConstraints do this, which is one of the reasons I recommended trying it. If your camera is based on a look-at system, you can use the smooth look-at constraint as well and it should all just work (you can set the constraints internal position instead of actually using a target in the scene).
Alright after a long sleep to clear my head, I see where you’re getting at. So I place the script on the headjoint so that it can be listed as the transform in the script, then I get the camera, which is part of the main body prefab, and when the animation plays, I set it’s position to the head joint so it’s there (although I’ll need to move it forward a bit).
There’s still the issue of matching the rotation of the headjoint properly.
I would think you want the script to be on the camera, then add a variable to the script like “public Transform headJnt;”. Then drag and drop the head joint on to this script which is on the camera. Now you have a reference and you can read it’s transforms.
If you want to place a precise offset from the head joint, you can make a new empty game object and make it a child of the joint. Call it something like “head_camRef” (assuming your naming convention works like this, that is up to you and just good work habit, it won’t break anything). This should be “physically” placed on the rig in the editor then drag and dropped in to the script. You could even include it as a null/locator from whatever 3D package you are importing from. Just do it in Unity first as a protortype and then take the local coordinates back to 3D. You would only do this if you want this object to be in the prefab so that it updates nicely if changes are made in the 3D app.
This is the part where I still am a little lost. You said you don’t want it to tilt with the head, so what do you want to inherit from the head rotations? Honestly, it seems to me the only head information that makes sense in the position and possibly where the head is looking, which means you need a 3D object to reference that is out in front of the head (eyes can stay locked on a point in space while the head moves, so you need the animation to consider this and output a look-at target from your 3D package - assuming you want to do this at all)
Secondly, how is your camera rotating now? Are you rotating it or looking at something?
If your camera is normally based on a look-at setup, then you can blend between the two quite easily. Before I say more, I think it would be best to clarify these points and get your thoughts.
Okay, I don’t want it to tilt on the Z and X axis because it make the camera look unstable, but it should follow the Y axis so that it turns with the Animation.