Unity Camera Maintain perspective while rotating - c#

I'm trying to write a camera script which will maintain the perspective point which I'm looking at while rotating.
public void RegisterRotationControls()
{
var horizontal = CrossPlatformInputManager.GetAxis(InputAxisName.Horizontal.ToString());
offsetX = Quaternion.AngleAxis(horizontal * turnSpeed, Vector3.up) * offsetX;
transform.position = player.position + offsetX;
transform.LookAt(player.position + PlayerOffset);
}
Currently this script is only working properly when the camera is looking at the player, however the camera has freedom to move position yet the rotation should maintain relativity to the center.
The top row dipicts the current behavior, while the bottom depicts what should be occuring

Manually calculating the rotation to move a camera in a circle while retaining its original (relative) facing is somewhat cumbersome, and requires you to do a lot more math than you need to.
A popular solution is to parent the camera under an empty GameObject, and centering that GameObject on the point/object you want the camera to rotate around. (This basically internalizes a lot of the calculations, letting the engine do the heavy lifting.) Then, you can locally translate the camera as needed and then rotate the container GameObject to move the camera in a circle.

Related

Problem with 3rd person camera rotation in unity

I'm trying to make the camera rotate around an object according to the movement of the mouse, and this is what I've come up with:
void Start()
{
direction = new Vector3(1f,0f,0);
direction.Normalize();
Debug.Log(direction);
mousePosCurr = Input.mousePosition;
}
void Update()
{
mousePosPrev = mousePosCurr;
mousePosCurr = Input.mousePosition;
mouseVector = (mousePosCurr - mousePosPrev);
mouseVector = mouseVector * sensitivity;
direction = Quaternion.Euler(mouseVector.y, mouseVector.x, 0) * direction;
direction.Normalize();
Debug.Log(direction);
this.transform.position = (target.position + tOffset + (direction * distance));
transform.LookAt(target.position + tOffset, Vector3.up);
}
Now, I'm sure that there are at least a million problems with this code, however the main one I'm facing right now is that the x rotation does not work correctly, unless the y rotation is in a specific direction.
What's happening is basically that the more I rotate in the y axis the more "inprecise" the x movement becomes. So for example, when the camera is south of the object I'm rotating it around, the x rotation works exactly as it should, you move the mouse up, and the camera points up and viceversa, but when it's west of it the movement of the mouse required for the vertical rotation is no longer vertical, but diagonal, and when it's north of the object the movement is inverted: moving the mouse up makes the camera point down and viceversa.
This seems most likely caused by the fact that regardless of the horizontal rotation of the direction vector, I'm still rotating along the x axis in the same direction, as if the camera was south of the object.
However, even having (hopefully) identified the problem, I'm still clueless as to how to solve it, so I'm asking for help here.
Note: I just noticed that I probably used the term "rotation" wrong in this post. When I write "rotation of the camera", I'm actually referring to the rotation of the "direction" vector.
alright so I am not the best in Unity so this might be completely useless but when I try to rotate my camera around an object I create an empty game Object and make the camera a child of it, like this
so that I can just just rotate the Game Object to rotate the camera around a fixed point maybe your code will work

Make GameObject's Y Rotation be the same as the camera's

I have a Cinemachine Freelook camera and i want it similar to Skyrim, where the character's rotation follows where the camera is pointing, but only on the Y axis so that i can move my mouse and look at the character from up and down;
This video demonstrates how MrKrabs can move in 3 dimensions, but won't turn.
I already tried creating a blank object and putting it at the center of the character, then using transform.LookAt(Camera) to make it point to where the camera is, and finally getting the Y value of the object's rotation, inverting it with -transform.position.y and applying it to MrKrabs, but it didn't work: it was jittery and overall just a bad user experience, is there a way to turn the gameobject based on the camera's rotation? (having the camera always watching his back)
Something like this should do the trick.
transform.rotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0);
Beware that if your camera is a child of your object it will rotate with it.

How can I do this kind of 3D camera movement in Unity?

This is a school project. I have a big cube which is like a planet. I have a small cube that can move on every side of the cube planet. The player moves forward automatically on its local z axis and I rotate it by 90 or -90 degrees to move left or right- but the player can control it like it's an up-down-left-right movement (made a script so they don't have to rotate the cube with 2 keys). It's like a Tron game so if I press the up key, I can't press down immediately because I leave a trail so I have to press the left or right keys. I have a problem with my camera movement. The camera follows the little cube from above, but I want it to lean a little when the little cube player gets close to the edge of a side so it lets me see the other side - where I'm planning to go.
I have a script where I use the center of the cube planet and the player's center as the direction vector and multiply it with the camera-player distance.
Vector3 direction = (target.position - center.position).normalized;
transform.position = target.position + direction * distance;
Then I point my camera's forward vector to the player cube's direction. I tried two codes but none of them worked perfectly. First I tried to rotate the camera with:
transform.rotation = Quaternion.LookRotation(-direction);
Shown in video. The problem with this is that since I don't tell the program the Up and Right vectors (I think), it sets them for itself to some random numbers so the camera sometimes do its own thing like it does a pirouette.
Next I tried this:
Quaternion rot = Quaternion.FromToRotation(transform.forward, -direction);
transform.Rotate(rot.eulerAngles, Space.World);
Shown in video. It's almost good but as the little cube moves, the camera starts steering slightly in the beginning and it gets worse and worse.
Do you have any ideas how can I make it work? I hope you understand what I would like to achieve. I'm trying to set the camera as to always show that when I press Up, my cube moves upward, when I press Right my cube always goes right etc. and when I reach the edge of my side the camera leans a bit towards the side I'm moving to.
Use dot product to find which orthogonal direction of your cube is closest to the camera's current up.
Vector3 closestToUp;
float greatestDot = 0f;
foreach (Vector3 dir in new Vector3[]{
target.forward, target.right, -target.forward, -target.right})
{
float curDot = Vector3.Dot(dir,transform.up);
if (curDot > greatestDot)
{
greatestDot = curDot;
closestToUp = dir;
}
}
Than, use that direction as the second parameter of Quaternion.LookRotation. This will keep its local up direction as aligned with the cube's "top" as possible given whatever new forward direction:
transform.rotation = Quaternion.LookRotation(-direction, closestToUp);

transform.rotate(vector3.forward * Time.deltatime) is rotating the bottom of the object, not center

I am new to unity and working on a game where a rocket flies up and down through a side-scrolling maze. Simple. The issue is the following code is rotating my rocket on the bottom rather than the center. This is similar to if you were to rotate on "pivot" mode in unity rather than on "center".
private void Rotate(){
float rotationMultiple = Time.deltaTime * rotationSpeed;
rigidBody.freezeRotation = true;
if (Input.GetKey(KeyCode.LeftArrow)){
transform.Rotate(Vector3.forward * rotationMultiple);
}
else if(Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(Vector3.back * rotationMultiple);
}
rigidBody.freezeRotation = false;
}
Your help means a lot as I try to get the hang of things.
In order to rotate around the center of the object, use Renderer.bounds and Bounds.center to get the center point and Transform.RotateAround to rotate the transform around the center point.
You're currently using euler angles to rotate the transform, but since your euler angles vectors only have one nonzero component, we can use them as axis parameters for RotateAround if you keep the axis and rotationMultiple separate:
private rend;
Start()
{
rend = GetComponent<Renderer>();
}
private void Rotate(){
Vector3 centerPoint = rend.bounds.center;
float rotationMultiple = Time.deltaTime * rotationSpeed;
rigidBody.freezeRotation = true;
if (Input.GetKey(KeyCode.LeftArrow)){
transform.RotateAround(centerPoint, Vector3.forward, rotationMultiple);
}
else if(Input.GetKey(KeyCode.RightArrow)){
transform.RotateAround(centerPoint, Vector3.back, rotationMultiple);
}
rigidBody.freezeRotation = false;
}
Be aware that if the shape/size of your renderer changes, the position of the center may change.
From Unity Answers:
When you modify the rotation of a Transform, the rotation happens around the local origin - that is, the center of the local coordinate system for the object. The actual middle of the object may not be placed at the origin - that depends on how it was modeled.
To make the rotation around the actual middle, there are a few things you could do.
Rotate around the bounds center. You can use collider.bounds.center to get the center of the world space bounding box of the collider of the game object, get the center of the local space bounding box of the mesh. In both cases, you can then use RotateAround to rotate around that point. Note that for the mesh bounds, you need to transform the center into global coordinates first.
Make an empty parent game object to the game object you want to rotate. Place the game object such that its middle is placed right at the origin of the parent. You can then rotate the parent game object instead using the normal ways to set the rotation of the transform.
(From https://answers.unity.com/questions/8599/how-do-i-rotate-and-object-around-its-center-in-sc.html)
This should help with your issue if I am understanding it correctly.
This was a great learning experience for me while playing around in the Unity editor. First, the “center” vs “pivot” example I gave shows just how new I am. I figure pivot was always the bottom, turns out that’s just where it was pivoting in this case.
Ultimately the fix I was looking for was Gameobject > center on children. This set the empty rocket wrapper on the center of its children and fixed the weird rotation I was getting.
Thanks all

XNA Monogame Mouse Projectile Click

I am coding for a game and I have it running fine and all, but I ran into the issue of projectiles. I have no idea of how to calculate the mouse position and send a projectile over. I've looked though many stackoverflow tutorials and many youtube videos, but they are either too vague or very very complex. Could someone please explain the process? I have been coding in C# for 2 years now, so I do have background knowledge. Please Help!
Well, you don't tell what kind of game you are working on and what the projectile you want to "send over", whatever that means but here are a few pointers, maybe one will help:
#1: If you are doing a 2D game, and you want to send a projectile from some point toward the mouse cursor, then you simply get the position of the mouse by calling GetState() on your MouseState instance, and reading it's X and Y properties, then get the direction of the projectile by subtracting the location of the origin of the projectile from the mouse position. Then you can move the projectile along that line by storing the time you launched it, subtracting that every frame from the actual time to get how long it has been on its way and then adding that many times it's speed times the normalized direction to its origin coordinates.
Example:
For this I assume you have a Projectile class that has Vector2 field called origin, one called direction, one called position, a float called speed and an int called startTime and you have an instance of it with origin and speed already set. Modify as necessary for your actual code.
MouseState ms = new MouseState();
in your Update method:
ms.GetState();
projectile.direction = new Vector2(ms.X, ms.Y) - projectile.origin;
projectile.direction.Normalize();
projectile.position = projectile.origin + projectile.speed * (gameTime.TotalGameTime.TotalMilliseconds - projectile.stratTime) * projectile.direction;
And then in the Draw method you just draw the projectile to the coordinates in projectile.position.
#2: You are doing a 3D game and want to shoot a projectile originating from your camera and directed toward whatever your mouse is pointing at. You will need to generate a Ray with something like this:
private Ray getClickRay(Vector2 clickLocation, Viewport viewport, Matrix view, Matrix projection)
{
Vector3 nearPoint = viewport.Unproject(new Vector3(clickLocation.X, clickLocation.Y, 0.0f), projection, view, Matrix.Identity);
Vector3 farPoint = viewport.Unproject(new Vector3(clickLocation.X, clickLocation.Y, 1.0f), projection, view, Matrix.Identity);
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
return new Ray(nearPoint, direction);
}
From there you can get the position of your projectile the same way as above, only using Vector3-s instead of Vector2-s.
#3: You want to simulate some sort of launcher and do a physical simulation on the trajectory of the projectile. Since I find this less likely than the first two I'll just describe the idea: you set a minimum and maximum rotation for your launcher along 2 axes, link each rotation to the ratio of the full width and length of your screen to the mouse position, and then use the angles and whatever other variables you have to calculate the direction and speed of the projectile at launch, then simulate physics each frame.
I hope one of these will help.

Categories