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\$
1
  • \$\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\$ Commented Jan 30, 2017 at 16:02

2 Answers 2

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

\$\endgroup\$
0
\$\begingroup\$

It looks like this old question meant "on the y-axis" as "Rotating only around the y axis (ie. yawing left and right), not rotating around the x axis (pitching up and down)".

That can be accomplished like this:

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

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

    // Look toward the target.
    var lookDirection = -toPosition;
    // Flatten this direction into the horizontal plane.
    lookDirection.y = 0;

    // Make a rotation that points straight out to the horizon in this direction.
    var yaw = Quaternion.LookRotation(lookDirection);

    // Set up a constant pitch if you want to look slightly downward.
    var pitch = Quaternion.Euler(pitchAngle, 0, 0);

    transform.rotation = yaw * pitch;
}

Here you can set pitchAngle in the inspector, or cache it in Start based on the initial camera pose.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .