1
\$\begingroup\$

I have two capsule objects in a split screen shooter. At this moment, the first camera follows player one, and has the target player two. The second camera follows player two, and has the target player one.

It feels a bit strange to play, because the game has different platforms with different height. How can I only follow the target along the y-axis?

The followed object shouldn't change at all. It should only look at the target. This is what I have, so far:

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour
{
    [SerializeField]
    private float distanceAway;
    [SerializeField]
    private float distanceUp;
    [SerializeField]
    private float smooth;
    [SerializeField]
    private Transform followedObject;
    [SerializeField]
    private Transform target;
    private Vector3 toPosition;

    void LateUpdate()
    {
        toPosition = followedObject.transform.position - target.transform.position;

        GetComponent<Camera>().transform.position 
            = followedObject.transform.position + toPosition.normalized *distanceAway;

        transform.LookAt(followedObject);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Is this a 2D game? Why do you want to only follow in Y position? There might be other solutions that suit what you want to do if you explain a bit further. (A screenshot would help too) \$\endgroup\$ – John Hamilton Jan 30 '17 at 16:02
0
\$\begingroup\$

You may want to try locking the other two positions and see if that works as a simple solution. So just set the X and Z axis = 0 at the same time you are updating where the Y axis would change and that should effectively stop it from changing those values which would make the Y be the focus.

Transform.LookAt(0,followedObject.position.y,0);

May look something like that if it's working.

There are some good Q/A and forums on unity's site that may help more if you need it. http://answers.unity3d.com/questions/458219/how-do-i-let-a-camera-follow-on-one-axis.html

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.