Camera doesnt move in z coordinate when player falls - c#

I have a problem with camera. The main problem is that if player goes down camera cant follow it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camera : MonoBehaviour
{
public Transform target;
public Vector3 positionOffset;
public float smooth = 3.0f;
void Start()
{
positionOffset = gameObject.transform.position - target.position;
}
void LateUpdate()
{
transform.position = Vector3.Lerp(transform.position, new Vector3(target.position.x +
positionOffset.x, positionOffset.y, target.position.z + positionOffset.z), Time.deltaTime
* smooth);
}
}
Here is the link of the problem:https://youtu.be/0RCEmrw3Xho

You missed changing the Y position with the target position. So the correct LateUpdate() should look like this:
void LateUpdate()
{
transform.position = Vector3.Lerp(transform.position, new Vector3(target.position.x + positionOffset.x, target.position.y + positionOffset.y, target.position.z + positionOffset.z), Time.deltaTime * smooth);
}

Related

My player rotation influnce my movement joystick how i fix this?

I crate a joystick and I calculate the direction the joystick make then with my direction I calculate my angle when I calculate my angle I duplicate my script so now one joystick for moving in script one and one joystick are for rotation in script 2, my problem is that when I rotate my player, let's say 90 degrees my move joystick moves the player to a different direction
The move joystick code have the angle calculate
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Unity.Mathematics;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
public class joystick : MonoBehaviour
{
public Transform player;
public float speed;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;
public Transform circle;
public Transform outerCircle;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.Log(Input.mousePosition.x);
if (Input.GetMouseButtonDown(0))
{
if (Input.mousePosition.x < Screen.width / 2)
{
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
circle.GetComponent<SpriteRenderer>().enabled = true;
outerCircle.GetComponent<SpriteRenderer>().enabled = true;
circle.transform.position = pointA;
outerCircle.transform.position = pointA;
}
}
if (Input.GetMouseButton(0))
{
if (Input.mousePosition.x < Screen.width / 2)
{
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
touchStart = true;
}
}
else
{
touchStart = false;
circle.GetComponent<SpriteRenderer>().enabled = false;
outerCircle.GetComponent<SpriteRenderer>().enabled = false;
}
}
private void FixedUpdate()
{
if (touchStart)
{
if (Input.mousePosition.x < Screen.width/2)
{
Vector2 offset = pointB - pointA;
Vector2 direction = Vector3.ClampMagnitude(offset, 1.0f);
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90;
Quaternion angleAxis = Quaternion.AngleAxis(angle, Vector3.forward);
movePlayer(direction);
circle.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y);
}
}
}
void movePlayer(Vector2 direction)
{
player.Translate(direction + player.rotation) * speed * Time.deltaTime);
}
}

How can I add the float to the position on Y?

The original code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloatInAir : MonoBehaviour
{
public float amplitude; //Set in Inspector
public float speed; //Set in Inspector
private float tempVal;
private Vector3 tempPos;
void Start ()
{
tempVal = transform.position.y;
}
void Update ()
{
tempPos.y = tempVal + amplitude * Mathf.Sin(speed * Time.time);
transform.position = tempPos;
}
}
But I want to keep the original position just changing the Y so I tried to change the transform.position line to :
transform.position = new Vector3(transform.position.x, transform.position.y + tempPos, transform.position.z);
But doing plus is wrong.
Use transform.localPosition instead of transform.position.
transform.localPosition will give the position of the transform relative to the parent transform.
Also You are assigning y position to tempPos.yso you should be adding tempPos.y and not tempPos.

Camera always behind player in Unity3d

I'm struggling with this for quit some time now. I have GameObject, being a sphere, which is my player on a 3d Terrain. I have a Camera which is always on a fixed distance from the player, follows it where it goes with below script:
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
void LateUpdate () {
transform.position = player.transform.position + offset;
}
So far so good. However what I actually want is that the camera rotates with the player, so it always looks into the direction where the sphere is moving, but always stays behind the player at the same fixed distance, so that the player is always visible in the camera view.
There are a lot of scripts available, but the problem with the onces I've seen so far is that the camera indeed rotate with the player, but because the player actually is a rolling sphere the camera view is rolling and turning as well.
The best script I found so far is below, but this one has the same problem as the other onces, the camera rolls with the player.
public Transform target;
public float distance = 3.0f;
public float height = 3.0f;
public float damping = 5.0f;
public bool smoothRotation = true;
public bool followBehind = true;
public float rotationDamping = 10.0f;
void Update () {
Vector3 wantedPosition;
if(followBehind)
wantedPosition = target.TransformPoint(0, height, -distance);
else
wantedPosition = target.TransformPoint(0, height, distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
//Quaternion ownRotation = Quaternion.RotateTowards;
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
}
Can anyone help me with this please?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public GameObject player;
public float cameraDistance = 10.0f;
// Use this for initialization
void Start () {
}
void LateUpdate ()
{
transform.position = player.transform.position - player.transform.forward * cameraDistance;
transform.LookAt (player.transform.position);
transform.position = new Vector3 (transform.position.x, transform.position.y + 5, transform.position.z);
}
}
My solution (based on #brennon-provencher answer) with smoothness and auto offset:
public class CameraFollow : MonoBehaviour
{
public GameObject target;
public float speed = 5;
Vector3 offset;
void Start()
{
offset = target.transform.position - transform.position;
}
void LateUpdate()
{
// Look
var newRotation = Quaternion.LookRotation(target.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, speed * Time.deltaTime);
// Move
Vector3 newPosition = target.transform.position - target.transform.forward * offset.z - target.transform.up * offset.y;
transform.position = Vector3.Slerp(transform.position, newPosition, Time.deltaTime * speed);
}
}
You need to move your camera position based on sphere movement direction -
public GameObject player;
private Vector3 offset;
float distance;
Vector3 playerPrevPos, playerMoveDir;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
distance = offset.magnitude;
playerPrevPos = player.transform.position;
}
void LateUpdate () {
playerMoveDir = player.transform.position - playerPrevPos;
playerMoveDir.normalize();
transform.position = player.transform.position - playerMoveDir * distance;
transform.LookAt(player.transform.position);
playerPrevPos = player.transform.position;
}
Edit 2: To fix flickering camera, try this -
void LateUpdate () {
playerMoveDir = player.transform.position - playerPrevPos;
if (playerMoveDir != Vector3.zero)
{
playerMoveDir.normalize();
transform.position = player.transform.position - playerMoveDir * distance;
transform.position.y += 5f; // required height
transform.LookAt(player.transform.position);
playerPrevPos = player.transform.position;
}
}

Make camera follow x-axis only

Trying to make it follow x-axis only. Only follows y when I replace with .y .y
It just doesn't want to work, no matter what I try. (Just started game dev today) I'm pretty noob at coding.
using UnityEngine;
using System.Collections;
public class FollowCamera : MonoBehaviour
{
public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;
// Use this for initialization
void Start()
{
targetPos = transform.position;
}
// Update is called once per frame
void FixedUpdate()
{
if (target)
{
Vector3 posNoZ = transform.position;
posNoZ.z = target.transform.position.z;
Vector3 targetDirection = (target.transform.position - posNoZ);
interpVelocity = targetDirection.magnitude * 5f;
Vector3 factorTowardsTarget = (targetDirection.normalized * interpVelocity * Time.deltaTime);
targetPos = new Vector3(transform.position.x, transform.position.y + factorTowardsTarget.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, targetPos + offset, 0.25f);
}
}
}
I don't really know much about what I'm doing either, but I gave it a shot. This is my first post.
From what I understand there's a certain order they have to be in, X then Y then Z. And the Y is what was getting the modifier added to it. I just put the modifier on the X position instead.
I tested it, it seems to work.
I just changed this part:
targetPos = new Vector3(transform.position.x, transform.position.y + factorTowardsTarget.y, transform.position.z);
To this:
targetPos = new Vector3(transform.position.x + factorTowardsTarget.x, transform.position.y, transform.position.z);
Result:
using UnityEngine;
using System.Collections;
public class FollowCamera : MonoBehaviour
{
public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;
// Use this for initialization
void Start()
{
targetPos = transform.position;
}
// Update is called once per frame
void FixedUpdate()
{
if (target)
{
Vector3 posNoZ = transform.position;
posNoZ.z = target.transform.position.z;
Vector3 targetDirection = (target.transform.position - posNoZ);
interpVelocity = targetDirection.magnitude * 5f;
Vector3 factorTowardsTarget = (targetDirection.normalized * interpVelocity * Time.deltaTime);
targetPos = new Vector3(transform.position.x + factorTowardsTarget.x, transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, targetPos + offset, 0.25f);
}
}
}
This code makes the camera follow the object only on the x position
public Camera MyCamera; // The camera you want to follow the GameObject
public GameObject ObjectToFollow; // What you want to follow
private Vector3 CameraPos; // Variable that contains the Cameras x,y,z position
void Start()
{
CameraPos = MyCamera.transform.position; // stores the Camera's position in the variable
}
// Update is called once per frame
void Update () {
CameraPos.x = ObjectToFollow.transform.position.x; // Change The X position on the camera variable to be the same as the ObjectToFollow X position
MyCamera.transform.position = CameraPos; // Moves the Camera to the new position
}

Move Camera in UnityScript 2d in C#

I have just started programming Unity 2d, and I have faced one large problem: How do I move the camera? The script is attached to the object "player". I want it to move with the player. Thanks!
/*
I
*/
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed = 10; //Float for speed
public string hAxis = "Horizontal";
void Start ()
{
//empty
}
void FixedUpdate ()
{
if (Input.GetAxis (hAxis) < 0) //Left
{
Vector3 newScale = transform.localScale;
newScale.y = 1.0f;
newScale.x = 1.0f;
transform.localScale = newScale;
}
else if (Input.GetAxis (hAxis) > 0) //Right
{
Vector3 newScale =transform.localScale;
newScale.x = 1.0f;
transform.localScale = newScale;
}
//Position transformation
transform.position = transform.position + transform.right * Input.GetAxis(axisName) * speed * Time.deltaTime;
}
}
Without any scripts, you could just drag the Camera GameObject to be a child of the player and the camera would start following the player position.
For a script, try this, set player as the target.
using UnityEngine;
using System.Collections;
public class SmoothCamera2D : 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 = camera.WorldToViewportPoint(target.position);
Vector3 delta = target.position - 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);
}
}
}

Categories