[英]Make camera follow x-axis only
Trying to make it follow x-axis only. 试图使其仅跟随x轴。 Only follows y when I replace with .y .y
当我替换为.y .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. 据我了解,它们必须遵循一定的顺序,依次是X,Y,然后是Z。Y是将修改器添加到其中的原因。 I just put the modifier on the X position instead.
我只是将修改器放在X位置。
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 此代码使摄像机仅在x位置上跟随对象
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.