3RD PERSON CONTROLLER in Unity - CAMERA
HTML-код
- Опубликовано: 21 сен 2024
- In this video (Episode 4) we implement movement animations for our player via animator values, that will be changed based on our input!
► PLAYER MODEL & ANIMATIONS
drive.google.c...
► JOIN OUR DISCORD
/ discord
► SUPPORT ME ON PATREON!
/ sebastiangraves
► ASSET STORE PAGE (Animations & Models)
assetstore.uni...
► FOLLOW ME IN INSTAGRAM!
/ tragicallycanadian
► EPISODE 5
• 3RD PERSON CONTROLLER ...
Ive spent the last 2 weeks messing around with 3rd person controllers from the asset store, tutorials and anything else I could find, but this is definitely the best I have found. This gives the exact player movement and camera rotation/collision I was looking for.
You are one bad ass dude!
Thank you!
For fixing the jittery camera, simply use Lerp to interpolate camera from its last position smoothly. The jitter happens because of the micro errors our hand naturally makes when moving things like a mouse. Here's my code at the top of the CameraManger script's RotateCamera method:
lookAngle = Mathf.Lerp(lookAngle, lookAngle + (_inputManager.mouseX * _cameraLookSpeed), _camLookSmoothTime * Time.deltaTime);
pivotAngle = Mathf.Lerp(pivotAngle, pivotAngle - (_inputManager.mouseY * _cameraPivotSpeed), _camLookSmoothTime * Time.deltaTime);
and here's the value I found to be good starting point for the variables:
_cameraLookSpeed = 15
_cameraPivotSpeed = 15
_camLookSmoothTime = 1
you can play around with these values to get even smoother results
jitter doesnt happen cause of the micro errors lol even if your move direction values are exactly 0 and 1 it still happens. probably because of the motion of the animations. can also be fixed by putting the player model and animation components under a empty with your scripts
@@PranjayMittal it does happen because of micro errors but probably not the jitter you are thinking of. I don't really stand behind using Lerp for smoothing the camera anymore especially if your game needs any form of accuracy. Players are already used to the slight jitter that happens because of your mouse. But anyways, to determine if we're talking about the same jitter, temporarily bind your camera controls to a keyboard key like A and D. If the jitter still happens, we're not talking about the same thing.
@@phee3D yeah i did that, that's why i said it happens even when its accurate. So yes i think we are probably taking about different things lol.
Thank you Sebastian for another great tutorial!
For anyone that might have an issue with the camera clipping into the player model when running towards the camera with S, change your camera follow speed. I found that 0.04 worked best for me.
EDIT: Might be worth noting my main camera is -4 on the z axis.
thanks bro
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
There's a reason why this video has zero Dislikes, Good work Boss Man thanks for the tutorial
Just wanted to let you know how much fun and meaningful all your tutorials have been so far, went from knowing only very basic C# concepts to understanding vectors, classes, quaternions, etc. just from watching your videos here and there. Thanks a ton and keep up the good work!!
This kind of feedback is really fantastic to read! I am very glad you took something away from the series my friend :^)
Was getting frustrated with the collision detection on the camera and literally was so confused. Rewatched you scripting it for at least 20 minutes was about to get off and take a break then realized i had misspelled default and position with defualt and postion god i almost lost my mind. I also had to rewrite the input actions because the Camera input caused the entire game to get a compiling error. Rewiring the input took away the camera problem but then the vectors on WASD were not being registered, a quick google searched show that all i had to do was restart unity and to myself i thought "ah yes unity at its finest." This entire journey just for character movement and camera control has been absolute blast despite the panic attacks, anxiety, lack of concentration, My daughter asking me what each word does and why im adding stars and plus sign if they're no numbers just words. Seriously enjoying this so far, i've come farther than any other tutorial has gotten me just with this one series. Good work man, you'll do good work some day or mindless work.
Hahahaah. Ahh yes, the rollercoaster that is learning Unity.
I don't know if anyone answered why you need Interpolate on but the reason that you have to turn it on is because the physics is on a fixed delta time versus LateUpdate or Update which have a unlocked delta time. So when the camera follows the player, it is seeing the rigidbody twice or three times depending on the FPS you are running at. When you enable interpolate, the rigidbody will fill in the in-between frames.
It's amazing to see you reply to comments even a year after the video, trust me you'll one day become the most popular unity programmer.
This is the best series I have ever watched on making a 3rd person controller. After hours of watching many other tutorials, your videos are the most helpful! Thank you very much for making such high quality videos!
Breathlessly waiting for the next episode. Have fingers crossed it will be soon.
Tomorrow my friend :^)
@@SebastianGraves when will the next ep come???
I didn’t realize you could use the slash notes that way, thanks!
It's still advantageous to know the basics setting up a simple camera like this one than diving cinemachine on the go
This is a really clean and tidy implementation of 3rd person controls, great if we need to make our own tweaks. Although this already behaves exactly as I wanted it, thanks for your great work dude.
Glad you enjoyed it man!
For anyone having an issue where the camera jumps further away from the wall whenever you collide with it:
Change:
targetPosition = targetPosition - (distance - cameraCollisionOffset);
To:
targetPosition = -(distance - cameraCollisionOffset);
I have changed it but still no effect, camera clips through the wall on collision and wont work properly
@@slavkodem70 Use targetPosition = targetPosition + (distance - cameraCollisionOffset);
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
Thank you
Awesome tutorial! It actually addressed the new input system so people can understand how to USE IT!!!
If Brackeys didt leave you guys should DEFINTLY collab this is Amazing!
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
@@JoskeVR I’m sorry I deleted it already it’s to long ago
Your series is great!. Btw Interpolation just sync's the rigidbody with the camera depth and makes it look smooth. I knew we would have a problem like this so i had set it to interpolate from start.
To add to this: It interpolates between the original position of the rigidbody and the new position at the end of the stutter, smoothing the transition. It operates in a similar fashion as Mathf.lerp()
Floats/Ints/etc have min & max values, so it's a good idea to limit them, so as to not reach those values. Odd stuff could happen ;D
if (lookAngle >= 360)
lookAngle = 0;
if (lookAngle
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
:') u know ur a great teacher when someone just wants to copy u but actually learns
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
@@JoskeVR he did make a script from what i remember, it was 3 years ago but i remember downloading it or copying it from his description. He gives it to u and teaches u
Camera Collision "FIX" 30:42
This is what I missed, hopefully it'll help someone else. When he changes the targetPosition part of the script to be a =-, well that already references itself so you need to move the other targetPosition.
So it goes from :
targetPosition = targetPosition - (distance - cameraCollisionOffSet);
and it needs to be THIS :
targetPosition =- (distance - cameraCollisionOffSet);
What happened was mine was :
targetPosition =- targetPosition - (distance - cameraCollisionOffSet);
Yeah, silly mistake. But hopefully this helps someone else!
And if you don't want your camera to start below the collision surface, replace the line :
cameraVectorPosition.z = Mathf.Lerp(camera.transform.localPosition.z, targetPosition, .2f);
with the following lines of code:
if (Mathf.Abs(camera.transform.localPosition.z) > Mathf.Abs(targetPosition))
cameraVectorPosition.z = targetPosition;
else
targetPosition.z = Mathf.Lerp(camera.transform.localPosition.z, targetPosition, .2f);
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
@@JoskeVR If you tried copy and pasting mrgarfields code it won't work because localPosition.z indicates its taking from a Vector3 but the float targetPosition isnt a Vector3, i'd just try to figure out a solution with chatGPT stating your problem and then trying it's solutions while applying some common sense and your own context.
If your camera automatically starts at the character's feet or somewhere you don't want it to. Set the transform of the camera pivot gameobject without the main camera parent, and then assign the main camera as the parent again.
By the way Thank you for this tutorials Sebastian :)
Fantastic tutorial, I am absolutely loving this series so far. you are a very underrated youtuber and tutor. Thanks a lot man
Much thanks to your great tutorials, Sebastian!
they have greatly expanded my game development knowledge, and I am always looking forward to watch more of your videos :)
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
just wanted to say thank you, i am making squid game in unity and your tutorials help me out a lot :)
Sounds like an awesome concept! Best of luck. :^)
Haha me too, green light red light?
Thanks for making this understandable. Awesome work.
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
One bug I noticed is that when you tilt the camera to look at far down as you can, this reduces movement speed because of the use of a rigid body moving in the direction of the camera.
How did you fix t?
@@zenpresentt In the PlayerLocomotion script, you have to change the HandleMovement() method; change "moveDirection = moveDirection * movementSpeed;" to "moveDirection = moveDirection.normalized * movementSpeed;"
omg thank you actual lifesaver@@4bot
YOU ARE MY HERO@@4bot
but I don't know why make .normalized if moveDirection was already normalized two lines up XD
For some reason in the top of the playermanager script I did not have public class playermanager. If you randomly stumble upon this I hope to have saved you some headache. Thank you btw Sebastian. Great video.
Much easier to understand the layermask concept in this tutorial than in the dark soul tutorials. Thank you so much. What’s next? Falling and jumping?
I believe sprinting, walking and running will be done in the next video! After that one, falling and jumping :^)
holy fuck you are the best, i forgot tutorials can be actually useful and mindful of possible mistakes and unclear code
Small update with an issue when camera is clipping (going backwards in direction towards camera).
Its happening because of Shperecast goes True by casting on the camera it self. It happens because the Collision offset is Player and Default whereas Camera Manager, Pivot and etc has the same Default layer. So to fix this you simply need to add a new Layer for walls on your level.
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
Just in case no one noticed it like I did for about 3 hours XD
For the Camera Collision
DONT FORGET TO DRAG the Main camera into the Inspector for the Camera manager : Camera Transform
Or else it wont move the camera when colliding
He does say to do that in the video but ONLY when he was typing out the Public Transform
He didnt state it at the end when he was testing out the camera because he also added the line in the Awake function to grab it instantly. XD
my camera still passing through walls :( how to fix that ?
@@nedsmamitag8518 Same thing is happening to me lmao
Thank you so much for making this! Now I can make a game about a chicken with a sword fighting people who want to steal their eggs
Hi man, first of all thank you, this series is everything I have been looking for. I won't lie I'm learning on my own, clocking 8+ hours a day for the past 2 1/2 months learning programming, c# and of course Unity as well as other things, but you nailed it where other have failed, giving me a sense of order and how to approach this task of creating a character controller from scratch, so once again I think you. However, I did came up with a problem, everything works fines except for the camera. i encountered 2 issues. One my camera can still go trough walls so it does bounce off if I am at a distance, but if I literally place the player next to the wall, it doesn't bounce it goes into the wall and then comes out as long as I move it out. The other issue i came up with is when looking up with the mouse or the joystick while moving forward, the camera moves too close to the player to a point where he player actually disappears from view, and from there if you stop, and look down, the actual player, that is the character model itself has rotated as if laying down to look up at the sky even after constraining the rigidbody on its xyz axis. I do have a picture of this issue if there is a way for me to get it to you. I do have to mention a couple of things, my walls are made from single blocks streched out unlike yours which is multiple blocks, and on the other hand my model/character is the ybot from mixamo and I'm using the adventures locomotion pack which up to this point I haven't had any problems with, well unless you count out the fact that I forgot to bake Y position on the idle animation in the beginning and that actually caused my player to float around a bit but since that, all of my animations have been baked to their respective recommended positions by unity, looped and set to humanoid rigs. So any help is much appreciated, I think it may be related to minimunCollisionOffSet values and such but I'm not too sure since I'm just beginning to grasp theses concepts, anyways sorry for the love letter, and once again thank you for the great content!
if collision doesn't work
1. cameraCollisionRadius 0.2f
2. check both Collision layers Default and Player
3. try away from the wall
1: In my case, the ray went in the wrong direction.
You can visually draw the ray using the Follow method:
Debug.DrawRay(cameraPivot.position, direction * Mathf.Abs(targetPosition), Color.green);
Add this method above the if-statement of Physiks.ShpereCast call.
If you want to check the length of the ray, add the DrawRay method below the if statement of ShpereCast.
2: Set the collision radius smaller than 2f, for example: 0.2f - 0.5f are good values.
As I use to say, Unity is not difficult, but it's complex. There are loads and tons of easy small bits that you must remember how to use and glue them together. I have a huge trauma with camera codes. Your code is a kick ass one. Thanks again. But everytime I spend almost 1 hour following a camera tutorial, I fell my Life force fading away, like I'm losing a few years of my life.
Time and Thousands lines of code later I think if all of this couldn't be solved with ONE SINGLE WORD.
I immediately shout "WHY THE F#%$^ JESUS CHRIST LICKING TADPOLES NONE USES CINEMACHINE! AHUHAUHAUAHUAH
Since we are in the learning path, I suggest we gather into the Discord Server and try to redo this tutorial with Cinemachine.
But hell yeah! It's a kick ass code for a camera inside the Dark Souls project. Nice Tutorial. The code is still fuzzy on my mind, but we got another stone at the Quaternion, Rotation, Position wall... THANKS A THOUSAND, ONCE MORE!
Amazing Thank you very much can't wait for more!
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
im having an issue where the camera wants to quickly move towards the player bc theres so much time between the character moving and the camera catching up, so it treats it as an object to have to close in on i think? im not sure where the issue is
yeah i think i'm having the same problem. like, when i run towards the camera, the camera jumps forward into my character as though the camera is colliding with something... let me know if you find a fix.
There's a comment just a bit down from this one that has the fix. Just remove the Player tag from the collision layers in Camera Manager. That should stop the camera from clipping out the player.
if hope you can put long code in gist, looks like i miss something the rorate its not working
the camera goes around the player but when I look up or down its only my cam that moves
I was having the same issue too but that's because I did both Y and Z for Camera Pivot and it caused this issue.
You have to set the Y for Camera Pivot (how high the camera will be) and the Z for the Main Camera (how far back is the camera). For this tutorial just set the Camera Pivot Y to 1.7 and Z to 0, then for Main Camera ensure the Y is still 0 but the Z to -3
My brain is burning after this video, But... IT WORKS!!!!!!! THANKS!
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
hey ya'll i have an issue where if you walk towards the camera aka "when the vertical input is negative", the camera clips to the head. This issue is supper annoying so I just took off the player layer from the Collision Layers parameters. idk if anybody with the same issue solved this a while back.
yeah, i'm having the same problem. i dunno why it's happening, but thats a good fix- i'll do that too
You legend, I was scratching my head on how it was happening. Thanks so much for the fix!
Snappy camera, awesome!
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
For those wandering why you did everything in the script yet your camera still passes through objects and doesn't detect a collision:
Ensure the Camera Pivot transform position Y = 1.8
AND
Main Camera transform position z = -3
My mistake was setting the y and z positional offset in the camera pivot gameobject. As a result the SphereCast method was not triggering maybe because the camera was spawned into a collision with the camera pivot since they were on top of each other.
Thanks, I wasn't sure why collision wasn't working and this was exactly it.
didnt work for idk why
@@mcb3920
how can i make look around when press right button on mouse?
Cleaner way to set rotation values (in my opinion) would be to just set them directly, instead of saving them in a bunch of unnecessary variables. :)
Ex:
lookAngle += inputManager.cameraInputX * cameraLookSpeed;
pivotAngle += inputManager.cameraInputY * cameraLookSpeed;
pivotAngle = Mathf.Clamp(pivotAngle, minPivotAngle, maxPivotAngle);
transform.rotation = Quaternion.Euler(0, lookAngle, 0);
cameraPivot.localRotation = Quaternion.Euler(pivotAngle, 0, 0);
a movimentação tá muito parecida com a do GTA SAN ANDREAS, bateu uma nostalgia...
if you see inside the walls you should change camera collision radius
from 2f to 0.2f
public float cameraCollisionRadius = 2f; // old one
public float cameraCollisionRadius = 0.2f; // new one
Also from the camera manager inspector in unity editor. The values must be same with code.
Thank you so much 😂😘
not every superhero wears cape 🦸🦸♂🦸♀
if your camera is glitching when you are running towards the camera then uncheck the player layer in camera's "collision layers"
Why are we separating the tweaking of our camera's height and depth into two different gameObjects (Camera Pivot and the Main Camera itself)? Seems pointlessly convoluted.
all amazing vids, so well explained
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
Hey dude I love your videos, great work you are sharing. I just wondered if the reason for the character stuttering could be the cameraManager.FollowTarget() inside LateUpdate(). I moved it to the end of FixedUpdate() and set Interpolate to None. Maybe Rigidbody does not need to interpolate movement between different frames then because it is in line with the physics updates (whereas using LateUpdate is not in line with physics -> interpolation needed)? Keep on learning dudes!!!
Hi! for some reason, I can't figure out where the error is... The camera continues to pass through the walls(
have the same problem
Thanks again for the tutorial, I'm really trying to grasp coding and the way your setting each script out means I actually remember what each line of code is for/doing! One issue I seem to have is my character judders when I'm holding W and move the camera left or right, so the player forward is snapping to the camera forward, I tried working it out but no luck, I'm guessing I need to smooth out the rotation some how??
edit, I changed (transform.position = targetRotation) to (transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, cameraLookAngleSpeed * Time.deltaTime);) as it seems to have help a bit.
Why didnt you use Cinemachine?
This is an great tutorial series. Thanks a lot for taking your time to make them and explain so much. A like and a sub from me.
But... I have a problem with the camera collision. When the camera hits a wall, it is pushed through the wall to the outside of the wall instead of it being kept inside. I tried tinkering with the radius and the offset and even with thicker walls. I watched the episode three times and can't see any typing errors. Help?
got the same problem
not solved :( sorry
@@thetra00 I had a simillar problem, fixed it putting cameraCollisionRadius to 0.2 instead 2, he even say that in the video but I totally didnt heard it lol.. hopefully it will help someone else :D
anyone, if the camera is still clipping through the walls change the camera collision radius to 0.2f
Loving the series, I was wondering if by any chance you have run into the problem where the up and down for the camera movement is inverted (i.e. i move my mouse up and the camera goes down and vice versa)
I've had this problem and fixed it by using: cameraInputY = -cameraInput.y; in the InputManager.cs
Yes finally
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
i have appeased the youtube algorithm gods
cool tutorial, I don't see why we want to check for collision with our player though. gave me a problem where the camera would snap inside the model because of the camera follow speed when running towards the camera
Did you happen to find a solution for this? I found that removing the player from the collision layers didn't stop this problem from happening (camera snapping inside the player when running towards the camera).
EDIT: Changed my camera follow speed to 0.01 and it got rid of the issue for me.
Nice work brother!! I rlly like these :D
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
Great tutorial. Thank you so much!
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
still Gold
When i go to where the camera is (press the S key) the camera just go inside the player when i release the S key its back to normal and when i pressed the other buttons its work fine only problem is with the S key
please help
Having the same issue. Any luck fixing?
EDIT: Changed my camera follow speed to 0.01 and it got rid of the issue for me.
remove "player" from camera collision layers
Comment "Camera looking up and down" must be for pivotAngle if I understand all right.
Hello! Impressive tutorial, could you answer a little question? I need my character not to change direction when the camera rotates, but I can't understand what part of the code is doing this, could you help me? Greetings, keep it up! I'm sure subscribers will rain
Comment to keep me Updated to this topic
For some reason, the camera clips into my player character's head every time I press play after we did the layers and set collisions and I'm not sure what's up. Other than that, fantastic video I look forward to binging the rest.
type this line in your start void:
deafultPosition = cameraTransform.position.z;
Hey for some reason camera movement with my mouse is faster than that with my gamepad's right stick. Is there a way around this or is it covered later on?
So underrated video!
Ah shit, It is going to be a challenge to code wallrun using this system xD
Hey man, great videos, do you plan to add some 3rd person combat in the future too?
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
How to make the camera rotation does not stop when it reaches the limit of the window when doing build.
By the way excellent tutorials very grateful for the excellent work you do.
I have an error and I don't understand pls help
Assets\Scripts\CameraManager.cs(82,133): error CS1503: Argument 6: cannot convert from 'float' to 'int'
and this is the script:
if (Physics.SphereCast(cameraPivot.transform.position, cameraColisionRadius, direction, out hit, Mathf.Abs(targetPosition), collisionLayers))
Make sure the collisionLayers variable is of type LayerMask and not float.
Your series is excellent!
I have one question to ask. my camera is still going through walls the collision detection is working fine and it also jumps off but still if I move around it still goes inside walls.
I'm having the same issue
I believe it is because the camera manager is at the floor while the camera itself is further back. I had to change my camera manager where the pivot and camera would be.
The problem with the camera is because the Rigidbody is updated in Fixed update and the camera in update or late update and they have diferent timing calculations.
Thx for the tutorial btw :D
Thanks for this series. I discovered it several days ago and am currently enjoying every bit of it.
Right now, there's a little problem I face : the camera's vertical movement makes it rotate on itself instead of orbiting around the target. I may have done something wrong but everything in the code looks fine to me.
could you please send the script cus mine doesn't work I'm begging you, just copy and paste.
I have the same exact issue did you fix it yet
Thank you very much for the video!
I encountered the following problem: when the character moves away from the original point, the camera radius becomes huge and rises to the top
Sorry for any mistakes :(
Not sure why, I have no Errors and i followed the vid and yet my camera wont go up and down and changing the pivotspeed and lookspeed doesnt do anything...
The camera follow was absolutely awesome, but the camera rotation was not my type when working with third person game... I mean it work but not smoothly like camera follow... I think there something to manipulate with camera rotation after using euler... Maybe i could use slerp but it will interrupt between another interpolation(smoothdamp)... Hmmm
the cameraManager script prevents my character from moving, it plays the animations, it rotates and all but prevents it from moving. when i remove the cameraManager from the camera, the character moves. its a problem i cannot seem to understand and may have to use a different tutorial for camera stuff.
Very good
How do you stop the camera from seeing through walls?
Thanks for the video 😀👍🏻
hello I have some sort of problems, the collision works fine but when its not colliding it pushes my camera far away what seems to be the cause?
EDIT:
i found my mistake it was a darn semicolon misplacement in the if statement for sphere cast
Hey man that serie is awesome, for you're next video can you explain: How to make footsteps?? pls
Easyway: add an AudioSource to the player,, put it a footstep clip, on loop and start on awake, disable it by default.
In the player script, create a bool "isMoving" (something like that) and a reference to the Audiosource, like with the rigidbody
make the bool true when inputs are not = 0 and false when it's = 0, at last enable the audisource when "isMoving" it's true!
You got your footsteps sound when your moving!
When I look down as far as I can and move, my movement slows down, I think it’s cuz the force is directed in the forward direction of the camera.
Is there a particular reason of why you don't use the Cinemachine package? Loving these videos!
thank you for the lessons
hey, i was wondering if you could post the code for the camera somewhere? ive been testing out a lot of different camera tutorials and i think it would be easier for me to understand if i just had the code infront of me to test ad poke at, otherwise ill just copy whats in the video, but it would be like the third time for me.
You can access it if you are participating to the pateron
@11:56 the playerControls.PlayerMovement.Camera cannot be found. Ive checked the name a thousand times. It founds the Movement just fine but not the camera I dont understand
Ever find a fix?
Having same problem right now
Hi buddy, great tutorials so far, watched a few of them (not all and not in order) just trying to see if this is what I need, so far so good, have you covered on top of the normal movements the character moving in the direction of the camera if that makes sense? Also, I read that you will be showing how to do this video with cinemachine instead of manually when can we expect this to release?
Once I have some time I have your dark souls vids bookmarked ready haha.
Hey dude, the player does move in respect to the camera's facing direction. I have however, made no intention to showcase this with Cinemachine, as it's relatively simple to set up!
Cheers!
Thanks a lot!
i want to add some code that resets the camera behind the player when ever I hit R3. anyone know how to help me with this?
In start method save the transform position of the camera in variable and when you hit R3 set the position of the camera with the variable
Loving the series!
Is the code going to be available somewhere, Sebastian (like via the Discord, or github perhaps)?
It can be a little tricky to go back and watch videos an extra 1-2 times to catch what part of the code I missed.
If not - I totally understand :). Thanks for the awesome series.
The Dark Souls Project was a little too advanced for me skill-wise, but this one is just perfect.
Keep it up!
Code is available on my Patreon for series supporters!
I am glad you are enjoying the series dude :^)
Cheers!
I followed every step in the last video but, when i hit play the only animation that displays is the running animation, the idle and walk dont work
same problem.
Is there a posibilty that the camera orbits more, so if you drive forwards and look right that you still go in the same direction but look to the side?
Hi I know im late to the show What a great tutorial so far i ve followed your video step by step and come across some problems and that is the camera falls to the players feet andthe player cant look up or down
I did everything you said in the video but the CameraCollision does not work for me do you know maybe what are the possible reasons for this?
Hi, did you somehow manage to resolve you problem ? Thank you
amazing collision better than cinemachine
HELP PLEASE COLLISION DONT WORKING
code:
public class CameraManager : MonoBehaviour
{
InputManager inputManager;
public Transform cameraPivot;
public Transform cameraTransform;
public Transform targetTransform;
private Vector3 cameraFollowVelocity = Vector3.zero;
public LayerMask collisionLayers;
public float cameraFollowSpeed = 0.2f;
private Vector3 cameraVectorPosition;
public float lookAngle;
public float pivotAngle;
public float maximumPivotAngle = 35;
public float minimumPivotAngle = -35;
public float minimumCollisionOffset = 0.2f;
public float cameraCollisionOffset = 0.2f;
private float defaultPosition;
public float cameraCollisionRadius = 2;
public float cameraLookSpeed = 2;
public float cameraPivotSpeed = 2;
private void Awake()
{
inputManager = FindObjectOfType();
targetTransform = FindObjectOfType().transform;
defaultPosition = cameraTransform.localPosition.z;
cameraTransform = Camera.main.transform;
}
public void HandleAllCameraMovement()
{
FollowTarget();
RotateCamera();
HandleCameraCollisions();
}
private void FollowTarget()
{
Vector3 targetPosition = Vector3.SmoothDamp
(transform.position, targetTransform.position, ref cameraFollowVelocity, cameraFollowSpeed);
transform.position = targetPosition;
}
private void RotateCamera()
{
Vector3 rotation;
Quaternion targetRotation;
lookAngle = lookAngle + (inputManager.cameraInputX * cameraLookSpeed);
pivotAngle = pivotAngle - (inputManager.cameraInputY * cameraPivotSpeed);
pivotAngle = Mathf.Clamp(pivotAngle, minimumPivotAngle, maximumPivotAngle);
rotation = Vector3.zero;
rotation.y = lookAngle;
targetRotation = Quaternion.Euler(rotation);
transform.rotation = targetRotation;
rotation = Vector3.zero;
rotation.x = pivotAngle;
targetRotation = Quaternion.Euler(rotation);
cameraPivot.localRotation = targetRotation;
}
private void HandleCameraCollisions()
{
float targetPosition = defaultPosition;
RaycastHit hit;
Vector3 direction = cameraTransform.position - cameraPivot.position;
direction.Normalize();
if (Physics.SphereCast(cameraPivot.transform.position, cameraCollisionRadius, direction, out hit, Mathf.Abs(targetPosition), collisionLayers))
{
float distance = Vector3.Distance(cameraPivot.position, hit.point);
targetPosition =- (distance - cameraCollisionOffset);
}
if (Mathf.Abs(targetPosition) < minimumCollisionOffset)
{
targetPosition = targetPosition - minimumCollisionOffset;
}
cameraVectorPosition.z = Mathf.Lerp(cameraTransform.localPosition.z, targetPosition, 0.2f);
cameraTransform.localPosition = cameraVectorPosition;
}
}
did you fix it?