paltrycomposedlarva
here’s the code for the camera:
please help.
sorry added the wrong video
revolvingscratchybighorn
Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.
There’s even a dedicated forum: Unity Engine - Unity Discussions
If you insist on rolling your own camera code, here is how you can get started debugging it:
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:
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:
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.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on 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.
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:
When in doubt, print it out!™
Note: the print()
function is an alias for Debug.Log() provided by the MonoBehaviour class.
here’s what’s happening (look to the right is the birds eye view)
webbedsphericalangelfish
0 is the same as 360, just like -180 is the same as +180.
If you want a different range you can offset it and modulo:
b is from -180 to +180
a is from 0 to 360
float a = (b + 360) % 360;
There is also Mathf.MoveTowardsAngle() to consider.
Again I caution you, this is not a long-term general solution and here is why:
Notes on clamping camera rotation and NOT using .eulerAngles because of gimbal lock:
How to instantly see gimbal lock for yourself:
All about Euler angles and rotations, by StarManta:
https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html
ah thank you I didn’t know this was a thing
thanks Kurt-Dekker I changed my code to use quaternions instead (here;s the code if your interested)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camfollow : MonoBehaviour
{
public Transform target;
public Vector2 dist;
public float smooth;
public Object capule;
private string stringtosearch;
private float dir;
public float todir;
private Quaternion lastangle;
void Start()
{
}
void Update()
{
transform.position = target.transform.position; //goes to target
transform.position += new Vector3(0 - Mathf.Cos(dir * Mathf.Deg2Rad) * dist.x, dist.y, 0 - Mathf.Sin(dir * Mathf.Deg2Rad) * dist.x); //offsets the z and y position by dist
if (Input.GetKeyDown("a"))
{
todir += 90;
}
if (Input.GetKeyDown("d"))
{
todir += -90;
}
dir += (todir - dir)/smooth;
lastangle = transform.rotation;
transform.LookAt(target, Vector3.up); //looks at target
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, transform.eulerAngles.z); //resets x rotation
if (lastangle != transform.rotation)
{
Instantiate(capule, transform.position, transform.rotation);
}
}
}