I want every step the player moves up the camera to move back one step in position z, and also the opposite when the player falls down.....
I really need someone to answer me I am new to making games and this problem stay with me for a long time so if anyone can help I will be so happy about that!!
So how to do it??
You can do this step to do the following task:
In your movement script get reference of your camera.
In your up movement function move your camera back in z axis by subtracting the amount you want camera to move :
float Step = 1; Camera.transofrm.position = new Vector3(Camera.transofrm.position.x,Camera.transofrm.position.y,Camera.transofrm.position.z-Step);
With this line of code your camera will go back in z each time player move up.
Do the same for fall down by just adding value to y axis of camera :
Camera.transofrm.position = new Vector3(Camera.transofrm.position.x,Camera.transofrm.position.y+Step,Camera.transofrm.position.z);
Related
I am working on my 2D game where I have my main camera moving horizontally from left to right. Instead of instantiate all game objects at the start of the scene, I want to spawn them as the camera moves close to their x positions.
I have a JSON like this to store the position of each GO that need to be spawned during runtime:
{
"apple":{
"xpos":13,
"ypos":2
},
"banana":{
"xpos":40,
"ypos":2
},
"pear":{
"xpos":25,
"ypos":2
}
}
Does anyone have a solution to trigger the instantiation when the camera's x position is equal or greater than the x position of each of the items? Suppose the camera starts moving from a x position of 0. Thank you in advance!
One solution could be:
Get the center of your screen (use an empty gameObject as a child of your camera or any other method of your suiting)
In Update() method check the distance between X position of the empty GO and xpos of your JSON if the distance is in between Screensize.x/2 and (Screensize.x/2 + offset) then instantiate.
Here is an image of what I'm trying to achieve
As you can tell it's a split screen game, the player is on the left, computer on the right.
there are 3 cameras in the game, main and two player cameras
the player camera MUST be independent of the player and CAN NOT be a child object of the player object, because the ball bounces and rotates while moving, the cameras must not.
when the balls change direction the camera must remain behind the player so the visual appears to show the landscape rotating with the player.
I've searched high and low for anything to put me on the right path but nothing seems to work right.
It should be a smooth transition so lerp and slerp are to slow for instant moving. I know LateUpdate will help with this.
If anyone can point me in the right direction I'd appreciate it.
Many thanks,
Paul
Have a script which takes in an object's position, in this case the player's ball, so you can code the camera as if it was a child of the object.
An simple example code for having a following camera would be something like...
FollowObject.cs
public Transform exampleObject;
private int offset = 5; //How far back the camera will be
void LateUpdate()
{
transform.position = new Vector3(exampleObject.transform.position.x,
exampleObject.transform.position.y,
exampleObject.transform.position.z - offset)
}
I have this 3D game I'm developing
In this game I am a roll of toilet paper
If I press space, I get yeeted towards the cursor
I started using raycasts to do this, but that didn't work out, AT ALL
So, how do I get the toilet paper to jump towards the cursor? (how do I get the direction for where the cursor is)
So from what I understood, you want your game object to move in 3D space to the direction of your cursor, which is in 2D space. You could try to get the game object's world location and use Camera.WolrdToScreenPoint(). This gives you Vector3 where x and y are screen coordinates and z is camera world z coordinate. Then you can get your cursor position with Input.mousePosition. Now you have two points that you can use to calculate direction. (You decide if you want to use camera z position or set it to zero.)
Vector3 payerScreenPos = cam.WorldToScreenPoint(player.transform.position);
Vector3 direction = (playerScreenPos - Input.mousePosition).normalized;
You need to have a reference to the current camera (cam) and to your player.
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);
What I am trying to achieve at this point, is the following:
Let's say that we have a Cube game object and a SteamVR player.
The Cube object is on position y = 100 and the SteamVR player is on position y = 0.
I want to make it possible that the player can zoom in on the game object by doing the following:
-> Pressing both triggers and bringing the controllers close to each other will zoom in.
-> Pressing both triggers and bringing the controllers away from each other will zoom out.
I think u understand the effect that I want to create.
For my project I am using the SteamVR Unity plugin.
Could someone say me if this is possible and give me some insight on how to do this?
Thanks in forward.
Have an if statement checking for two inputs and move the camera position in the forward direction close and closer to the target. If you want to save the original camera position before zooming just store the Camera.main.forward before incrementing it.
pseudocode
public SteamVR_Input_Sources LeftInputSource = SteamVR_Input_Sources.LeftHand;
public SteamVR_Input_Sources RightInputSource = SteamVR_Input_Sources.RightHand;
public Vector3 currentZoom;
public Vector3 zoomAmount;
void update(){
if( SteamVR_Actions._default.Squeeze.GetAxis(LeftInputSource) && SteamVR_Actions._default.Squeeze.GetAxis(RightInputSource)){
currentZoom.forward += zoomAmount.forward; //increment zoom by whatever amount while
triggers are held
Camera.main.transform.forward = currentZoom;
}
}
I have not tested this, hence why I labeled it pseudocode, however, I hope this helps!