How can I make a camera in Unity, that repeats the rotation and position of another camera on all three axes?
I'm thinking about portals - how to repeat players camera movement and rotation by another camera which renders texture for the portal, to create a realistic effect while player moves - that there is a whole new scene behind the portal.
Imagine I have a player camera and another camera in another place in the scene. The second camera may have different position and rotation initially. But when the player camera rotates 90 degrees to the left, the second camera should add +90 degrees to the left to its current rotation.
And the same with movement, so if the player moves 1 meter forwards, the camera moves 1 meter forwards from its current position.
You can create a script that takes a transform as external input which copies the values from one object to the other. If you want to keep the offset, so just look and move in the same direction, but not be at the same location that is also possible.
The following script lets you mimic another object:
public class Mimic : MonoBehaviour
{
[SerializeField]
private Transform other;
private Vector3 offset;
private void Start()
{
offset = transform.position - other.position;
}
private void Update()
{
transform.rotation = other.rotation;
transform.position = other.position + offset;
}
}
Related
I am making a game in unity where the player can move around a map and when they hold down the spacebar the camera rotated 180 degrees so that they are looking behind them.
When the player presses and holds down the space bar i also want the camera to move right above the player (at the moment the camera is positioned behind the player looking at the player) looking back.
This is the code that i have to rotate the camera as well as reposition the camera:
Quaternion halfRotation = Quaternion.Euler(14, 180, 0);
if (Input.GetKey(KeyCode.Space))
{
if (!globalScript.lookBack)
{
globalScript.lookBack = true;
transform.rotation *= halfRotation;
transform.position = new Vector3(transform.parent.position.x, transform.parent.position.y + 1, transform.parent.position.z);
}
}
else if (globalScript.lookBack)
{
globalScript.lookBack = false;
transform.rotation *= Quaternion.Inverse(halfRotation);
}
I change the position of the camera which is a child of the player in the second if statement when the space bar is pressed. this moves the camera to where i want it to be when looking back but i am struggling to figure out how to put it back to its original position when space is released.
The position i want it to revert to is (0,4,-9)
How do i revert it back to this location?
Before starting the camera flip action, you can store the position of the camera in a vector variable and then set it back to that variable after the action is over.
I am working on a game which has strong base on spherical gravity. But somehow my code is not working as expected. So please have a look at my code and tell me how can I make my spherical gravity work.
public class CircularGravity : MonoBehaviour {
private Rigidbody2D rigid;
[SerializeField]
Transform planet;
[SerializeField]
float acceleration = 0.81f;
// Use this for initialization
void Start () {
rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
rigid.AddForce((planet.position - transform.position).normalized * acceleration);
transform.rotation = Quaternion.LookRotation(planet.position - transform.position, transform.up);
}
}
Problem elaboration. Project is 2d. it contains a circle sprite as a planet with a collider and hexagon has a player(just a prototype) with collider and rigidbody. This script is attached to the player(hexagon). According to my logic rigidbody should apply a force to the player and push it towards the planet and it should face the planet. So even if the player is on the downward side of circle it shouldnot fall instead it shall be pushed toward the planet. But all the colliders are being neglected and player is just going to strange position
First things I would check...
Make sure the origin of your sprite is actually in the center of the image. If it's in the corner the player will be pulled toward the corner.
Make sure both the planet and player have rigid bodies and colliders. Click on the collider and make sure the edge lines up with you shapes.
Make sure both the planet and the player have their z set to zero. Lock z in both rigidbodies.
I have an enemy on Unity, the enemy follows the player and when player is at a specific distance i want the enemy run X units without stopping. The idea is that the enemy run, then he gets tired and the Player can attack him from behind.
How can i make enemy run X distance forward without stopping?
How can i make enemy run X distance forward without stopping?
Depends on a lot of things, how is the movement handled in your game? is it 2D? 3D? etc. ...
If you use a character controller on your Enemy GameObject for example (in 2D)
Vector2 moveDirection = Vector2.zero;
void Update(){
CharacterController player = GetComponent<CharacterController>();
if (player.isGrounded)
{
// constantly move horizontally
moveDirection.x = valuespeed;
player.Move(moveDirection * Time.deltaTime);
}
}
This is just some basic theory. FOr 3D, you add a dimension and choose the direction by using the current forward vector or so.
As for checking the X value, what you could do is that in your Enemy class, when the "running mode" is triggered, you save the origin position and in the update, you check the distance between CurrentPosition and Origin (Vector3.Distance(......)) and as long as the distance is smaller than X, you let him run. If not, you are not in "running mode".
I am trying to make a game where the camera follows the user, as they move around. I made the camera a child of the player and in the editor, the camera rotates around the player fine. When I play the game (in Unity) and rotate the player, the camera literally rotates instead of rotating around the player to keep the same regular following distance.
I use transform.Rotate() to rotate the player by the way.
In Summary:
Looking for camera that follows player in 3D game, that does not look different when the player turns.
issue is that the camera appears in the editor to rotate around the player perfectly, but not when transform.Rotate() is called on it during runtime in Unity.
All help is appreciated, tyvm for help.
I made the camera a child of the player and in the editor
Everything went down by doing this. You don't make the camera a child if you want it to follow the player.
What you do is to get the distance between the camera and the player in the Start() function. This is also called offset. In the LateUpdate() function, continuously move the camera to the Player's position, then add that offset to the camera's position. It is as simple as that.
public class CameraMover: MonoBehaviour
{
public Transform playerTransform;
public Transform mainCameraTransform = null;
private Vector3 cameraOffset = Vector3.zero;
void Start()
{
mainCameraTransform = Camera.main.transform;
//Get camera-player Transform Offset that will be used to move the camera
cameraOffset = mainCameraTransform.position - playerTransform.position;
}
void LateUpdate()
{
//Move the camera to the position of the playerTransform with the offset that was saved in the begining
mainCameraTransform.position = playerTransform.position + cameraOffset;
}
}
I'm using my camera as a child gameobject of my ball, so when I move my ball camera comes with him. But the problem is I'm using rigidbody.addForce(), so when ball rotates the camera rotates with it, too.
So what should I do not to rotate my camera but only move it with my ball?
void FixedUpdate()
{
rigidbody.addForce(Input.getAxis("Horizontal"), 0, Input.getAxis("vertical"));
}
There are several ways to solve this, I'll list a few.
The first, if you don't care if your ball rotates or not, you can just disable the rotation on the ball. If you open the Constraints section in the rigidbody component, you can freeze the rotation so that the ball won't rotate.
Alternatively, you can write a script that keeps the camera always oriented a certain way. Depending on if you want the camera to rotate around the ball on any plane depends on the way you would implement this.
The third option, which is cleanest, is to not have the camera be a child of the ball. A minimal component to do this would look like this:
public class TargetFollow : MonoBehaviour
{
public Transform Target;
public float DistanceFromTarget;
void Update()
{
transform.position = Target.position + new Vector3(0, 0, DistanceFromTarget);
transform.LookAt(Target);
}
}
Just drag your ball into the 'Target' slot in your component. Keep in mind, this is super bare bones. You may want to add more variables to better control the direction the camera should be from the ball, or perhaps something that grabs a snapshot of the direction and distance the camera is from the ball in the Start method.