Instantiate GameObject while Camera Moving - c#

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.

Related

how to make camera move back every time player moves to up?

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);

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.

Unity Sub Camera following Player with rotation problem

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)
}

How do I make the player move towards the cursor Unity3D

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.

Control player z axis with mouse x axis

I'm trying to move the player to the position of the mouse in my game.
Right now movement along the x axis works fine, but I want the mouse y axis to control the characters movement along the z axis (because of my top-down camera's y being world z).
Right now mouse y controls player y, which looks like this in game.
And the code for it looks like this:
public Vector2 mousePos;
private Vector3 playerPos;
void update()
{
// Get mouse position from player
mousePos = Input.mousePosition;
// Move player with mouse
playerPos = new Vector3(mousePos.x, 0, mousePos.y);
transform.position = Camera.main.ScreenToWorldPoint(playerPos);
}
I then tried to just swap the y and z like this
playerPos = new Vector3(mousePos.x, mousePos.y, 0);
But instead of allowing me to control the z axis this snippet instead causes the player to lose all movement.
I'm very new to coding so I might be missing something completely obvious. What am I doing wrong?
Unity version: 2018.4.21f1
The example you gave isn't working because you're providing a z coordinate of 0 and when called on a perspective camera, Camera.ScreenToWorldPoint does some vector math, so in your scene view, your player is probably floating right on top of your camera. I can't explain the actual math because I don't understand it, but luckily for both of us, we don't need to! Essentially, the z coordinate is saying how far away from the camera to place the point, and since the view frustum of a perspective camera gets narrower closer to the camera, placing it at 0 means there's nowhere for it to go. how moving the object farther from the camera requires it to move farther to match the mouse position.
The bigger problem is that your method is wrong and there's a better way. Camera.ScreenToWorldPoint converts a mouse coordinate to a world space coordinate depending on what the camera is looking at, so you're essentially flippng the y and z coordinate, then feeding it into a method that figures out what coordinates need to be flipped.
It looks like this script is attached to your player gameObject, so it should look like this:
Camera cam;
[Range(1,10)] //this just creates a slider in the inspector
public int distanceFromCamera;
void Start()
{
//calling Camera.main is a shortcut for
//FindGameObjectsWithTag("MainCamera")
//so you should avoid calling it every frame
cam = Camera.main;
}
void Update()
{
//This will work for both perspective and orthographic cameras
transform.position = cam.ScreenToWorldPoint(
Input.mousePosition + new Vector3(0,0,distanceFromCamera));
}
If your camera is going to move closer or farther from the plane, make sure to add something that keeps the distance from camera updated.
That said, for most games having the player tied to the mouse cursor isn't usually what you're looking for. Generally you want some kind of input to move the player in some direction a certain amount. If that's what you're looking for, this part of the space shooter tutorial is a good introduction to player movement (though the code itself may be outdated).

Categories