I used This code for my MainCamera for following the player in my 2d game in Unity5 :
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour {
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
public Transform target;
// Update is called once per frame
void Update ()
{
if (target)
{
Vector3 point = GetComponent<Camera>().WorldToViewportPoint(target.position);
Vector3 delta = target.position - GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}
}
It work fine But player is in middle of screen allways . i wan player be in down of screen and my sprite for show Earth of my game will stick below the camera . i mean better in following pictures:
What I Want :
The Result :
You can add a vertical offset to the calculation. Just adding it to destination should do that I think.
Vector3 destination = ...
destination.y += someOffset;
transform.position = Vector3.SmoothDamp(...);
Otherwise you could also add an empty gameobject to the player gameobject and use that as your target.
One thing that you might need to consider is the resolution.
Related
I have a ball and want the main camera to follow it around so I attached a script to the camera:
public class Tracker : MonoBehaviour
{
public GameObject target;
void Update()
{
this.transform.position = Vector3.Lerp(this.transform.position, target.transform.position, .5f);
}
}
Where the target is the ball gameobject.
I want the camera to have the same x and y coordinates as the ball but keep its original Z.
Not sure what to do, maybe there's a different way of approaching this?
You can create you movement Vector3 with a constructor
for ex:
Vector3 newPos = new Vector3(target.transform.position.x,target.transform.position.y, transform.position.z);
this.transform.position = Vector3.Lerp(this.transform.position, newPos, .5f);
The target is a simple 3d cube.
This screenshot showing the turret in the hierarchy and the script attached to it and the target.
The script is attached to the Turret child :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateTurret : MonoBehaviour
{
[SerializeField]
private float turnRateRadians = 2 * Mathf.PI;
[SerializeField]
private Transform turretTop; // the gun part that rotates
[SerializeField]
private Transform bulletSpawnPoint;
//private Enemy target;
public GameObject target;
void Update()
{
TargetEnemy();
}
void TargetEnemy()
{
if (target != null)
{
Vector3 targetDir = target.transform.position - transform.position;
// Rotating in 2D Plane...
targetDir.y = 0.0f;
targetDir = targetDir.normalized;
Vector3 currentDir = turretTop.forward;
currentDir = Vector3.RotateTowards(currentDir, targetDir, turnRateRadians * Time.deltaTime, 1.0f);
Quaternion qDir = new Quaternion();
qDir.SetLookRotation(currentDir, Vector3.up);
turretTop.rotation = qDir;
}
}
}
When running the game the cube is not moving the turret is not moving. Only if I'm moving the target cube in the scene view window drag it then the turret rotate but also the turret is a bit behind never facing the target cube.
What I need it to do is when running the game the cube will start make circles nonstop around the turret with random speed and the target cube also should move up down with random height between min/max height.
And the turret should rotate facing the target according to the target height and speed.
Make an empty gameobject same position with turret, then make target child of that empty object and place it to where you want to rotate it, then rotate empty parent object with that sciprt
private int _minY, _maxY;
private int _targetHeight;
private const float Tolerance = 0.1f;
private void Start()
{
_minY = -10;
_maxY = 10;
_targetHeight = Random.Range(_minY, _maxY);
}
private void FixedUpdate()
{
var randomSpeed = Random.Range(2, 4);
transform.Rotate(0,randomSpeed,0);
if(Math.Abs(transform.position.y - _targetHeight) < Tolerance)
_targetHeight = Random.Range(_minY, _maxY);
transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, _targetHeight, transform.position.z), randomSpeed/10f);
}
I have created a walking character in Unity 3D and I watched a tutorial to make a script that makes the player follow the camera which is good but I want the camera to be lower done and to be rotated more backwards so that the camera can see more of the world as right now not much is visible passed the player. I have attached a link to an image of what it looks like now.
https://i.imgur.com/jQ6efAJ.png
as you can see you can't see much of what's in front of the player.
Next I will attach the script and hopefully you can show me what code needs adding or changing to allow my changes to be possible.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform playerObject;
public float distanceFromObject = 6f;
void Update()
{
Vector3 lookOnObject = playerObject.position - transform.position;
lookOnObject = playerObject.position - transform.position;
transform.forward = lookOnObject.normalized;
Vector3 playerLastPosition;
playerLastPosition = playerObject.position - lookOnObject.normalized * distanceFromObject;
playerLastPosition.y = playerObject.position.y + distanceFromObject / 2;
transform.position = playerLastPosition;
}
Thank you in advance for helping me. This is for a school project so I really hope that your solutions are great and can't wait to hear from you!
You're looking into adding an offset to the camera relative to the look position of the camera. What you need to do is to offset the playerObject.position when you assign it to the lookOnObject.
Add a new vector3 and call it as an lookOffset. It will now become lookOnObject = (playerObject.position + lookOffset) - transform.position. It will act as a pitch control for your camera.
Use distanceFromObject to control the z position of the camera.
[EDIT]
Here's the altered script that I made. As mentioned above, you need to offset the position that the camera will look to. In this script, instead of a Vector3, I used float:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;`
public class CameraFollow : MonoBehaviour
{
public Transform playerObject;
//Controls the pitch of the camera where it rotates
//in X axis
public float pitchOffset = 0;
//Controls the yaw of the camera where it rotates
//in Y axis
public float yawOffset = 0;
public float distanceFromObject = 6f;
void Update()
{
//Creation of the look offset relative to the
//Observable position
Vector3 lookOffset = playerObject.position;
lookOffset.y += pitchOffset;
lookOffset.x += yawOffset;
Vector3 lookOnObject = playerObject.position - transform.position;
//Replaced playerObject.position with lookOffset
lookOnObject = lookOffset - transform.position;
transform.forward = lookOnObject.normalized;
Vector3 playerLastPosition;
//Replaced playerObject.position with lookOffset
playerLastPosition = lookOffset - lookOnObject.normalized * distanceFromObject;
playerLastPosition.y = playerObject.position.y + distanceFromObject / 2;
transform.position = playerLastPosition;
}
}
It could be done with Vector3 or Vector2 where you store the pitchOffset, and yawOffset to x and y fields.
Okay, I made some progress with the look towards me I'm able to get the enemy ship to follow the player and the laser guns as well could use some guidance how to get the laser to kill the player ship and prompt the lose and the 'R' for restart messages Aanty insight how to go about it is welcomed.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyManagement : MonoBehaviour
{
[SerializeField] GameObject deathFX;
[SerializeField] Transform parent;
// The target marker.
[SerializeField] Transform target;
// Angular speed in radians per sec.
[SerializeField] float speed;
// Start is called before the first frame update
void Start()
{
AddSphereCollider();
}
private void AddSphereCollider()
{
Collider sphereCollider = gameObject.AddComponent<SphereCollider>();
sphereCollider.isTrigger = false;
}
void Update()
{
Vector3 targetDir = target.position - transform.position;
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
Debug.DrawRay(transform.position, newDir, Color.red);
// Move our position a step closer to the target.
transform.rotation = Quaternion.LookRotation(newDir);
}
}
You need to give it a radius for collision detection.
sphereCollider.radius = 10.0f;
This is for a 2D platform game.
I don't want the camera to move up the Y axis when the player jumps. I only want it to move when the player to the upper part of the screen so it can scroll up to vertical platforms and ladders.
Does anybody know what to enter in the code and the Unity editor so that can be done?
Here's the code I have so far in the camera script.
public class CameraControl : MonoBehaviour {
public GameObject target;
public float followAhead;
public float smoothing;
private Vector3 targetPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
targetPosition = new Vector3 (target.transform.position.x, transform.position.y, transform.position.z);
if (target.transform.localScale.x > 0f) {
targetPosition = new Vector3 (targetPosition.x + followAhead, targetPosition.y, targetPosition.z);
} else {
targetPosition = new Vector3 (targetPosition.x - followAhead, targetPosition.y, targetPosition.z);
}
transform.position = Vector3.Lerp (transform.position, targetPosition, smoothing * Time.deltaTime);
}
}
I guess you have a bool tied to the jump which triggers the jumping animation.
So, in the Update() of the camera you can do something like this:
void Update() {
// Update camera X position
if (isPlayerJumping) return;
// Update camera Y position
}
This way, you update the Y position of the camera only if the player isn't jumping, while still updating the X position in all cases (even while jumping).