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;
}
}
Related
Hey i am making a unity game called Cube-Runner. There is a cube and obstacles and you have to go between them. Not going into the game a lot but the problem is how to follow the player. I can't parent it as if i do that while the cube falls the camera will also move with the rotating cube and will make the player(At least myself) dizzy.
Please give me a script
There needs to be a offset Vector3 which i can change from the inspector.
The offset Vector3 may work.
It should be written in C#.
NOTE: I AM NEW TO C# AND UNITY DO NOT JUDGE BY QUESTION
if you dont want to make the player the parent of the camera, then you can do this :
Create a C# script called CameraMovement and attach it to the camera
add this to CameraMovement
using UnityEngine;
class CameraMovement : MonoBehavior
{
public Transform player;
public Vector3 offset;
void Update()
{
//get the players position and add it with offset, then store it to transform.position aka the cameras position
transform.position = player.position + offset;
}
}
click on the camera and look at the inspector, you should see that there is a script called CameraMovement and 2 fields : player and offset. assign player with your player (drag and drop) and offset with the relative position between your camera and the player (where the camera is with the player being the center).
and you're done, play the game and see the results
You could try using the cinemachine tool, it will make you camera follow smoothly to the player. You could check any tutorial on youtube but I recommend you to check the one "Brackeys" did. No code needed for cinemachine
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;
}
}
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'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.
I'm creating a project in which I have a sphere GameObject which has empty game object as a child. A line renderer component is attached to the this empty game object.
I have made the sphere to look at the touch direction so that the line renderer of the child object also faces towards the touch position. It works fine when the sphere is rotating in one axis.
But the problem occurs when I apply a force to the sphere and it starts rotating in all axis just like a normal ball. But the line renderer attached to it is stuck to its face and when the sphere faces downwards, the line renderer goes into the ground.
Code for Line Renderer.
using UnityEngine;
using System.Collections;
public class LineRenderer : MonoBehaviour {
private LineRenderer lineRenderer;
private Vector3 finalTouchPosition;
// Use this for initialization
void Start () {
lineRenderer = gameObject.GetComponent<LineRenderer> ();
}
// Update is called once per frame
void Update () {
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
lineRenderer.SetPosition (0, ray.origin);
if (Physics.Raycast (ray, out hit)) {
lineRenderer.SetPosition (1, hit.point);
}
}
}
Code for rotating the object with touch.
if(theTouch.phase == TouchPhase.Moved)
{
Vector3 tempTouch = new Vector3(theTouch.position.x ,theTouch.position.y, myCamera.position.y - targetObject.transform.position.y);
fingerPosition = Camera.main.ScreenToWorldPoint(tempTouch);
targetObject.transform.LookAt(fingerPosition);
isRotating = true;
}
I want the line renderer to always be parallel to the ground but originate from the sphere or face of the sphere. I've tried many things. But still haven't found the solution.
Your question seems a little confusing, but it seems like you want a ball to rotate around a line. The ball to rotate at its own will and the line to just stay relative to the ball and no rotation.
If this is correct. What I suggest is having two objects. the ball and then an empty/invis object. The ball rotates and moves and just takes the empty object with it by movement, no rotation. Put the line renderer on the empty object and in the hierarchy have invis object be the parent of the ball. Move the empty object with the ball physics and bamn rolling ball, that responds to touch.
Sorry, if Im bad at explaining. And if you dont know when you parent something in the hierarchy they move together.