Camera Control variable in Unity? - unity3d

I have a script that controls camera. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void LateUpdate()
{
transform.position = player.transform.position + offset;
}
}
I drag and drop script to camera, it it works. But I have not added any variable for camera, so unable to understand how it is controlling camera?
e.g. player.transform.position >> This is position of player.
but transform.position >> this is controlling the camera. How? Shouldn't it be something like camera.transform.position?

transform.position is the transform.position of the game object to which the script is attached.
Here the script is attached to the Camera game object, so transform.position is the position of the Camera game object.

Related

How do I rotate an object's parent without the child being moved along?

In my game on unity 2d I have spaceship and a planet. The planet is orbiting a star so I made a script that parents the planet to the player when I get within a range so the planet doesn't fly past or into the player. This script makes the player move with the planet so they land on it and fly around it easily.
Here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParentPlayer : MonoBehaviour
{
[SerializeField] GameObject Player;
private float Dist;
[SerializeField] float Threshold;
private CircleCollider2D ParentTrigger;
// Start is called before the first frame update
void Start()
{
ParentTrigger = GetComponents<CircleCollider2D>()[1];
ParentTrigger.isTrigger = true;
ParentTrigger.radius = Threshold / transform.localScale.x;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject == Player)
{
collider.gameObject.transform.SetParent(transform);
}
}
private void OnTriggerExit2D(Collider2D collider)
{
if(collider.gameObject == Player)
{
collider.gameObject.transform.SetParent(null);
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, Threshold);
}
}
The problem is that as the planet rotates it ends up moving the player that has been parented to it. How can I make the planet's rotation not affect the position and rotation of the player, but still make the planets position affect the position of the player?
This might not be what you're looking for but I'm going to add it reguardless. Given that the Child-object will follow the parent, I would suggest putting the planet and spaceship as a child of the empty gameobject. Then you could rotate the the planet object, and if planets are in movement, you could add the movement to the parent object.
How about instead of parenting the player, write a script to set its position to the planets + some offset, and then the rotation wouldn't be an issue. Alternatively, if the player doesn't rotate at all, add constraints to its Rigidbody. Maybe something like this:
player.transform.position = planet.transform.position + offset;
Hope that works!

unity2d move a game object towards a random position

i have a satellite which i let spawn randomly in a different script by just using the method Satellite() and i want it move towards the bottom of the screen but not towards the middle but again a random position of the ground. the problem is that satellitedirection is always 0. also, if i somehow would manage to get the problem solved, wouldn´t every satellite on the screen move towards the same position?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SatelliteBehaviour : MonoBehaviour
{
[SerializeField] Rigidbody2D satelliteRB;
[SerializeField] Transform satelliteFirePoint;
[SerializeField] GameObject satellitePrefab;
float satellitedirection;
void Start()
{
}
void Update()
{
transform.Rotate(0,0,80*Time.deltaTime);
//satelliteRB.velocity = -transform.up * 5;
transform.position = Vector2.MoveTowards(transform.position, new Vector2(satellitedirection, -15), 1 * Time.deltaTime);
Debug.Log(satellitedirection);
}
public void Satellite()
{
Instantiate(satellitePrefab, new Vector2(Random.Range(-18,18),10), Quaternion.identity);
satellitedirection = Random.Range(-18, 18);
}
}
The Instantiate() method returns a game object but it is not assigned to anything. Assign a var and set its position and rotation.
GameObject satelliteObj = Instantiate(satellitePrefab, new Vector2(Random.Range(-18,18),10), Quaternion.identity);
// random rotation
satelliteObj.transform.rotation = Quaternion.Euler(new Vector2(Random.Range(-180, 180), Random.Range(-180, 180)));

How do I make a camera that points towards where the player is going and is back and up and pointed down?

it is a ball that actally rolls so I can't just put a child camera on with an offset and call it a day so instead I created this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camera : MonoBehaviour
{
public GameObject Player;
public Vector3 lastpos;
public Vector3 cameraxyz;
public Vector3 camerarotationxyz;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 currentDirection = Player.transform.position - lastpos;
transform.rotation = Quaternion.LookRotation(currentDirection-camerarotationxyz);
transform.position = currentDirection + cameraxyz;
Vector3 lastPos = Player.transform.position;
}
}
and attached it to an empty game object made the game object a child of the ball and then made the camera a child of the empty game object
which half works the empty game object will all always rotate to have it's z axis aligned with the origin meaning the offset for the camera is wrong and it won't look at where the ball is going but will look towards the ball
this is how I set the hierarchy up (I put the script on the empty game object)
https://i.stack.imgur.com/sbiMt.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camera : MonoBehaviour
{
public GameObject Player;
public Vector3 lastPos;
public Vector3 cameraxyz;
public Vector3 camerarotationxyz;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 currentDirection = Player.transform.position - lastPos;
transform.rotation = Quaternion.LookRotation(currentDirection - new Vector3(0,currentDirection.y,0));
Vector3 newPosition = currentDirection + cameraxyz;
transform.position = newPosition;
lastPos = Player.transform.position;
transform.position = Player.transform.position;
}
}
taking the Vector3 off of lastPos and capitalizing mistakes leads to a gameobject with an incorrect offset and rotation to stop it traking the y axis (as I can change which whay is up and change the y to be parrallel to gravity using an external script) I did (currentDirection - new Vector3(0,currentDirection.y,0) the new Vector3 is needed and so are the zeros as a float nor int cannot be use to subtract from a Vector3 then I did transform.position = Player.transform.position; so that the empty game object is correctly put on the ball then to get the camera to move with the correct offset I made the camera a child of the emptygameobject

LookAt is centering at feet of player

When I use LookAt the camera seems to look at the targets feet. I would like the target to be closer to the bottom of the screen. (e.g. look at the head instead).
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour
{
private GameObject player;
private void Start()
{
player = GameObject.FindWithTag("Player");
}
private void LateUpdate()
{
Vector3 offset = new Vector3(0,2.0f,-4.0f);
transform.position = player.transform.position + offset;
transform.LookAt(player.transform.position);
}
}
How can I make it so the camera stop centering at the feet?
I created an empty game object as a child of the player, and then adjusted the position of that. Then in my script I set it to look at that empty object instead of the player. This gives me full control of what position the camera looks at on the player.

Unity camera following rolling ball. No rotation and no moving in Z-axis on camera

So, i'm building a 3D runner and i've got a lot of problems making the camera following the ball. I've been on google for a few hours and can't find anything that's not outdated or fills in the things I need. I want a camera that follows my ball but goes straight in the X-axis. When there are the stairs the camera needs to follow on Y axis.
I just don't want my camera to rotate (since my object is a rolling ball) and to move in the Z-axis.
var myPos : Vector3;
var myPlay : Transform;
function Update()
{
transform.position = myPlay.position + myPos;
}
This is what I already have. It doesn't rotate, but it follows on the Z-axis. I don't want that.
http://prntscr.com/9pmypz This is what it looks like in the Inspector.
As per Unity3D's Roll-A-Ball tutorial:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
}
}
I can't comment #HDL_CinC_Dragon answer because of my low reputation, but I wanted to add that using the public access modifier is a really bad habit. Instead you should use the private access modifier with the SerializeField attribute on the field, like this:
public class CameraController : MonoBehaviour
{
[SerializeField]
private GameObject player;
private Vector3 offset;