I have a problem with camera and character, the current setup for now is that the character is a 2d sprite in a 3d world, and the camera keep a distance from the character (is a basic offset), and the character look at the camera at all times (the character being a 2d sprite it need to face the camera all the time). The problem happens with the current setup, because the camera offset is not following the character, but simply staying at a certain distance from the character, that makes the camera to stand still if the character moves very slightly horizontaly, and the character horizontal movement became a circle around the camera (because it is aways looking at the camera his left and right changes inward toward camera), but is enables it to rotate around the character using the mouse, however, if I make the camera follow the character, it looses it ability to rotate around since now is locked at a certain position. I do know the problema but I lack the knowledge to solve it, because to solved it I need to make the camera follow the character but maintain its ability to rotate around, but the current setup now make those two things combined virtually impossible.
private void LateUpdate()
{
//Responsible to get the where to look at for the character.
rotAux = new Vector3(playerCamera.position.x, this.transform.position.y, playerCamera.position.z);
//Just makes it look at the proper direction.
transform.LookAt(2 * transform.position - rotAux);
}
// Update is called once per frame
void FixedUpdate()
{
//Makes the character move.
rigidBody.linearVelocity = transform.TransformDirection(new Vector3(moveVal.x * moveSpeed, 0, moveVal.y * moveSpeed));
//Responsible for make the camera move around player.
playerCamera.transform.RotateAround(transform.position, Vector3.up, turnSpeed * mouseLook.x * Time.deltaTime);
}
Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.
I’ll take a look at Cinemachine, if it is more intuitive then most likely I’ll use it. I’ve found that there was a problem in my initial setup, the camera was supposed to rotate around the character (using the mouse or right stick on a controller, like any other third person game), but the character was rotating according to the camera, not the other way around, thus resulting in the player left and right directions rotating around the camera (the pivot became the camera) instead being on thenself (the character pivot point). I’ll try one more time to adrress this and if I find too much of a hassle for a simple thing that cinemachine could solve then I’ll change (just because I do like to solve problems and do things on my own to understand how things work)
I fundamentally respect this worldview and I want to support you.
Just know that camera stuff really is tricky, as noted above.
If you wanna understand just about anything with Unity, it really is an incredibly simple game engine to work with: everything is a GameObject, a Component, or an Asset… that’s it! And code can interact with each of those things in various ways, and relationships can exist between these things, either automatically via the engine or programmatically via your code.
With diligence and patience and good debugging skills, you can unravel just about any mystery you might encounter.
The good news is that ANYONE can learn debugging, and it is one of those skills you will always be learning forever into the future, if you so choose. Here’s my blurb on debugging:
Time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Here is how you can begin your exciting new debugging adventures:
You must find a way to get the information you need in order to reason about what the problem is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the names of the GameObjects or Components involved?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
Visit Google for how to see console output from builds and even to debug actual builds on device.
If you are running a mobile device you can also debug and view the console output. Google for how on your particular mobile target, such as this answer for iOS:
Or this answer for Android:
If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
If your problem is with OnCollision-type functions, print the name of what is passed in!
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
If you are looking for how to attach an actual debugger to Unity:
“When in doubt, print it out!™” - Kurt Dekker (and many others)
Note: the print(); function is an alias for Debug.Log() provided by the MonoBehaviour class.