Home > Enterprise >  Tilting the camera towards a specified direction in Unity3D
Tilting the camera towards a specified direction in Unity3D

Time:08-11

hey yall i need some help, ive been trying to figure out how to make my camera tilt towards a specific direction correctly but been having some weirdness to it. specifically its in the direction my player has been hit, like in this video describing how minecraft used to have this tilting camera to express where damage was taken from

my idea was to collect the direction the attack was from, get the cross product of it and transform.up to get an axis i can rotate around, and then use Quaternion.AngleAxis to tilt my camera in that direction, but with this implementation it doesn't seem to react to me turning around in place, always tilting in the same odd direction

this is my script that handles being attacked and initiates the camera tilting

// Vector3 punchSource is from attack instance
Vector3 punchPosition = new Vector3(punchSource.x, transform.position.y, punchSource.z);
Vector3 punchDirection = (punchPosition - transform.position).normalized;
Vector3 crossAxis = Vector3.Cross(punchDirection, transform.up);
pa.punchRotation = Quaternion.AngleAxis(45f, crossAxis);

and this is whats relevant in my camera controller

public Quaternion punchRotation;
...
punchRotation = Quaternion.RotateTowards(punchRotation, quaternion.identity, 45f * Time.deltaTime);
...
cameraRotation  = punchRotation.eulerAngles;
transform.eulerAngles = cameraRotation;

and an example of my problem

https://imgur.com/a/p9gza4v

CodePudding user response:

If you use the animation in Unity, Start recording and try to move your camera as you like. you can save that clip. This way you have full control of how you want the camera to shake. Trigger it every time that your character is attacked. You can also check out cinemachine

CodePudding user response:

You seem to be looking for Transform.LookAt on any script attached to the object that you want to another object, simply call transform.LookAt(target) in the update function and your object will rotate to the target. This WILL make your object rotate on the x, y and z so if you want restrictions, you'll need to add those.

Also, another answer is talking about how to do a very complicated version of this to record a video. If you are looking to make a video, simply run your scene, then go to your scene manager and you can use the right mouse button, mouse movements and the WASD keys to traverse your scene like a floating orb.

  • Related