Unity general questions

Comments

@Thelangfordian the problem isn't in doing barrel rolls, my current setup already allows for that. The problem I'm trying to solve for is to correct for rolling when my controls are in neutral - so a self-correcting force is what I'm looking for, not for the player to be able to counter the rotation manually. Does that make sense?

My code that I attached already does do that, but it also has some unforeseen weird vector force in there that makes it accelerate in odd directions when it shouldn't (it should just be purely rotational, no acceleration).

So how do you self-correct a torque in one axis? I think answering that alone will do.

To reduce the torque you would need to add a counter torque (with dampingFactor) until the rigidbody.angularVelocity is zero. To apply this to a single axis you need to convert the angularVelocity to local coordinates, apply dampingFactor, then convert back to global coordinates.

You need to get an instance of the class that you want to call a function from with GetComponent, so you could have a script attached to your camera and then call Camera.main.GetComponent<MyScript>().MyFunction(). The other way is to declare the function in a class as public static, then you can call it from anywhere with MyClass.MyStaticFunction(), but you can't access instance specific variables or functions from that function. If you just want to use declarations from another namespace, you would use using.

camera.WorldToViewportPoint should give you a point in your viewport space, where bottom left is (0,0), top right is (1,1), and the z coordinate is the distance in front of you. So when you're checking if something's within the camera view, you're checking if the z is positive (i.e. it's in front of your camera rather than behind it), and that the x and y each fall between 0 and 1.

I'd debug it by printing out what viewportPoint is giving you, and seeing why it is or isn't falling within what you expect.