I have an object that moves forward backward left and right and i want the camera to move up and down not left and right i tried using the z value but i have an error (im not the best at coding) https://i.stack.imgur.com/riVoA.png if you have a answer please help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class followplayer : MonoBehaviour
{
public Transform playerchar;
public Vector3 cameraOffset;
// Start is called before the first frame update
void Start()
{
cameraOffset = transform.position - playerchar.transform.position;
}
// Update is called once per frame
void Update()
{
Vector3 newPosition = playerchar.transform.position cameraOffset;
transform.position.z = newPosition.z;
}
}
CodePudding user response:
You cannot change transform.position this way. That's the correct way to do so:
transform.position = new Vector3(transform.position.x, transform.position.y, newPosition.z);
CodePudding user response:
Use the Quickway:
var tPos = transform.position;
tPos.z = newPosition.z;
transform.position = tPos;