while following some guides to make an object move, I have this issue.
So, all objects on the scene are 0, 0, 0, on rotation except for the camera. The camera, then is 0, 180, 0. The script says - for a object to move to the left
:
void Update () {
transform.Translate(Vector3.left * (ScrollSpeed * Time.deltaTime));//access transform component and moves it left by 20 times the frame rate
}
So technically it moves left, but the camera perspective is at 180 degrees to look at the game from the front. So is my only solution to rotate the platform on Y to match the perspective to make it in reverse so when working on things I dont have to keep using negative numbers to do the opposite of the actual scene?
Instead of using Vector3 static variables which are define in the world space, you can use the transform of the Camera, so the moving directions are depending on the Camera. That is to say in your case:
void Update () {
transform.Translate(
-Camera.main.transform.right * (ScrollSpeed * Time.deltaTime));
}
Notice the - in front of Camera.main.transform.right, the opposite of the right direction is the left direction ;-)
If you have several Cameras in your scene, use a reference to the Camera you want instead of Camera.main
I found the issue.
So the original scene itself was 0, 0, 0, but I needed to play the scene -180 on the Y axis and face the camera at 0, 0, 0, this way the only thing rotated is the design of the background.
Related
I am starting out to create a minigolf game in Unity. I want to aim by draggin the screen with my mouse which rotates the camera around the ball. Basically I want to draw an aim line (line renderer or any other good method) from the ball to where the camera is pointing. Right now I just draw the line I want from beginning, but in the end I want it to rotate with the camera like the red line in picture below.
The code for rotating the camera is working great, where previousCameraPosition is set when I click the mouse and Rotate() is called when mouse button is being held down:
private void Rotate()
{
// Get the movement vector
Vector3 cameraMoveDirection = previousCameraPosition - mainCamera.ScreenToViewportPoint(Input.mousePosition);
// Rotate around the ball
mainCamera.transform.position = ballObject.transform.position;
// Make the rotations
mainCamera.transform.Rotate(new Vector3(1, 0, 0), cameraMoveDirection.y * 360);
mainCamera.transform.Rotate(new Vector3(0, 1, 0), -cameraMoveDirection.x * 360, Space.World);
// Set the camera a certain amount away from the ball
mainCamera.transform.Translate(new Vector3(0, 0, -ballObject.transform.position.z*2.5f));
// Update the previousCameraPosition during the movement
previousCameraPosition = mainCamera.ScreenToViewportPoint(Input.mousePosition);
}
I tried to create a helper object which was set to rotate exactly like the camera which made the line exactly like I wanted it, except it was backwards (180 degrees wrong, facing into the camera instead of away) and it was 1 unit long since it was normalized. I am stuck on this for far too long now, any help or guidance would be very appreciated.
First you can simply use the camera's forward vector without having to deal with any Quaternion rotation at all:
var forward = Camera.main.transform.forward;
Then what you actually seem to want is not exactly this direction but rather map it onto a flat XZ plane:
var actualForward = Vector3.ProjectOnPlane(forward, Vector3.up).Normalized;
then finally you can use this direction to draw your line e.g. between the points
var startPoint = ball.position;
var endpoint = startPoint + actualForward * lineLength;
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.
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);
I'm Using Unity 2d and I made it so that the camera turned with the mouse in the x axis no problem. But I want the camera to only turn a small amount. The mouse needs to have full freedom to move but the camera needs to stop at a certain point.
I tried making an empty game object and set it so if the camera position equals that gameobject position the camera would move slightly back but that resulted in a lot if camera clipping. Also tried altering speed but of course that doesn't work. Does anyone know how I can do this without camera clipping?
Sorry for the lack of code as my computer has no interest access. Currently my code basically is:
transform.position = newvector3.Movetowards(
Input.GetAxisRaw("mouse X") * speed * Time.DeltaTime,
0f,
Input.GetAxisRaw("mouse Y") * 0 * Time.DeltaTime
);
That might be a bit inaccurate but that's the basic of it
To prevent clipping while using limited camera movement try something like this
transform.position = new vector 3(Mathf.Clamp(transform.position.x, MIN_X, MAX_X), 0, 0);
Of course replace MIN_X and MAX_X with the numbers of the minimum and maximum position you want the camera to be able to rotate to. This is off the top of my head so the code might not be 100 percent accurate. You can find Mathf.Clamp in the Unity docs if you're having trouble.
So I'm trying to make a third person controller that uses the PS4 controller with the left stick controlling the rotation and movement and the right stick controlling the camera separately. my problem right now is that when I apply this script to my game object it causes the player to Jitter back and forth. Any thoughts as to why or how I can fix it thanks.
void Update ()
{
//Defaults to the left Stick
float hAxis = Input.GetAxis("Horizontal");
float vAxis = Input.GetAxis("Vertical");
Vector3 NextDir = new Vector3(hAxis, 0, vAxis);
if (NextDir != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(NextDir); //this rotates the character correclty but causes jitter
transform.Translate(NextDir.x * Time.deltaTime * 5, NextDir.y * Time.deltaTime * 5, NextDir.z * Time.deltaTime * 5, Space.Self);
}
}
}
Perhaps the "jitter" you see is the game code accurately rotating the object according to the data it is getting from the joystick. I am guessing that by "jitter" you mean the rotation is bouncing back and forth between clockwise and counter-clockwise sporadically, when you think that you are smoothly rotating the joystick in one direction (clockwise or counter-clockwise). (If not, please describe the jitter.)
If this is the case, since you are reading the joystick at every frame, the joystick is probably picking up values that cause the vector to bounce back and forth. So, try adding a tolerance and only taking action if the vector changes by a certain amount. Or, only accept vectors that cause the rotation to continue in the same direction (CW or CCW) as the previous frame, unless the change is more than some certain amount, which would indicate that the player really does want to start turning back in the other direction.
Try increasing the value of the dead property in the horizontal and vertical axis. This should mean the joystick has to be moved further out of it's resting position before reporting a value.
It's located in Edit->Project Settings->Input modify the value for the second occurrences of those two properties ( the first two are for the keyboard )