Minimise Y movement in Follow Cam & Teleport

Imagine a character going from left to right, and right to left, and sometimes jumping up and down.

I want the camera follow this guy, but only in the left and right, with a little lag, exaggerated by the fact the camera is looking at this hero, but I don’t want the camera to follow in the up and down, nor look up and down.

How to do this with Cinemachine?

Teleport, in this same space, sometimes happens, from left to right, and vice versa, at which point their should be no noticeable change in the camera relative to the hero.

Is this also possible?

Any and all suggestions and mirth/mockery greatly appreciated.

  1. Give the vcam a big vertical dead zone.
  2. There is an API call to support teleporting, but only in CM 2.1.12 and up. Which version are you using?
1 Like

I think I’ve found the Cinemachine Teleporter, a Warper function from the bottom of the page linked at the bottom of this…

 public override void OnTargetObjectWarped(Transform target, Vector3 positionDelta)

I’ve got this working…

THANK YOU!!!

!!! Defender Style, Seamless Looping World Teleportation !!!

here’s the Eastern Portal, of which the Western is the same, but for the bool saying it’s not the East Portal:

And here’s the Telporter Script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class backupTelporter : MonoBehaviour {


public CinemachineVirtualCamera vCam1;
CinemachineComponentBase myCamera;

Transform otherTransform;
Rigidbody2D otherRB;
Vector2 RB2D_Velocity;
float RB2D_rate_Of_Spin;

Vector3 teleport_Destination;
Vector3 teleport_origin;
Vector3 teleport_origin_in_portalSpace;
Vector3 teleportDistance;

public bool EastPortal;
public GameObject Destination;

void Start ()
{
    myCamera = vCam1.GetCinemachineComponent<CinemachineComponentBase>();
}

private void OnTriggerExit2D(Collider2D other)
{
    otherTransform = other.transform;
    otherRB = other.attachedRigidbody;

    if ((EastPortal && otherRB.velocity.x > 0) || (!EastPortal && otherRB.velocity.x <0)){
        DoTeleport(otherRB);
        }
}

void DoTeleport(Rigidbody2D RB2D_toBe_Ported)
{
    RB2D_Velocity = RB2D_toBe_Ported.velocity;
    RB2D_rate_Of_Spin = RB2D_toBe_Ported.angularVelocity;
    teleport_origin = RB2D_toBe_Ported.transform.position;
    teleport_origin_in_portalSpace = transform.InverseTransformPoint(teleport_origin);
    teleport_Destination = Destination.transform.TransformPoint(teleport_origin_in_portalSpace);
    teleportDistance.x = teleport_Destination.x - teleport_origin.x;

    RB2D_toBe_Ported.Sleep();
    RB2D_toBe_Ported.transform.position = teleport_Destination;
    RB2D_toBe_Ported.WakeUp();
    RB2D_toBe_Ported.velocity = RB2D_Velocity;
    RB2D_toBe_Ported.angularVelocity = RB2D_rate_Of_Spin;
    myCamera.OnTargetObjectWarped(otherTransform, teleportDistance);

    Debug.Log("Teleported Other!");
    }
}

Here’s the camera settings:

1 Like

@Gregoryl Is there a way to do the above with a LookAt, and just a slight bit of slop in the way the Follow works, so there’s some slight perspective change as the hero moves too fast for the camera Follow?

Possibly, but you might need to hack the code ever so slightly

I’ve got it working… except I can’t constrain the camera to a Y setting. The camera is transforming with the target’s Y movement. Other than that, it’s working well.

Any idea how I can lock the vCam’s y to a set “latitude”?

@Gregoryl

This sort of works, but has a few glitches, that are huge and disturbing, and has a little noise that’s kind of cool:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class constr : MonoBehaviour {

CinemachineVirtualCamera myCam;
CinemachineComponentBase myBase;
public Rigidbody2D  RB2D;
CinemachineTransposer myTransposer;
    void Start ()
    {
        myCam = GetComponent<CinemachineVirtualCamera>();       
        myBase = myCam.GetCinemachineComponent<CinemachineComponentBase>();
        myTransposer = myCam.GetCinemachineComponent<CinemachineTransposer>();
    }
   
    void Update ()
    {
        myTransposer.m_FollowOffset.y = RB2D.position.y * -1;
    }
}

The big, disconcerting glitches, that are infrequent, is Cinemachine flipping the Z value of the camera for a couple of frames. Not me, I promise. I’m not touching the Z value.

This is beginning to exceed the imaginative capacity of my tiny cranium. Any chance that you can throw together a little package that demos this so I can have a look?

I’m new to Unity. So not yet sure how to share, or make much of anything. I’m still working in the same project I first started in, just adding stuff…

where/what/how to share a project?

I’m assuming you’re working in a test scene… with minimal artwork.
If not, it would be a good idea to make that, with a simple shape for a player, and minimal or no scenery. Wire in the camera and player controls, and the teleport stuff, so that the camera behaves the same way and shows the issue.
Save the scene.
Now do this:
3465454--275008--upload_2018-4-17_16-12-39.png

And in the resulting popup select all the stuff that needs to be included in the package: scene, scripts, and (if needed, textures, and other assets - try to have as little as is reasonable).

3465454--275009--upload_2018-4-17_16-15-34.png

Then hit export, and send me the result, which I will import into an empty project (you can test that before sending it to me).

1 Like

Thank you!!!

This will come in very handy, soon… I’m sure to make more problems for myself!

This one, I have managed to fix. I tried a bunch of stuff, two things worked best…

  1. Moved the offset maintainer to different updates, FixedUpdate seems to work best. I thought lateUpdate would work better, but it doesn’t.

  2. Best change: Turned Binding Mode to “Lock to Target, No Roll”. I completely forgot about this setting. This was the cause of the glitch. Now it can’t flip around to the reverse Z position relative to the target, and never glitches.

YESSIR !!!

@Gregoryl

Is “look ahead” incompatible with Warping? It wigs out, pretty hard. Goes the wrong way for a bit, by a good amount, and then tries to come back after a bit. Can’t find a way to stop this wigging out.

oh crap, I think you’re right, they don’t play nice. Will fix that. Thanks for letting me know!

1 Like

No worries.

In general, just wow, because it all “just works™”!

THANK YOU!!!

@Deeeds I take this back - in fact they should play nicely. Are you using the teleport code that you posted above? I don’t trust it. OnTargetObjectWarped() expects a delta position in world space. Your code is doing some funny business with the positions… why doesn’t it just pass (destTransform.position - srcTransform.position) as a parameter?

Can you have a read through this, help me understand what @MelvMay is trying to say? It might also help explain why I’m doing the translation of the transform from its position in one portal to the other ie. the object is moving so fast that it’s somewhere well outside the portal, not at an edge, by the time the physics engine realises its exited the portal and needs to be teleported.

Rather than have the teleport push it back to a position inside the destination portal on teleport, I use that world position and transform it to origin portal coordinates, then use that in the destination portal coordinates to find the world space to teleport to.

Both portals are the same size, orientation and shape.

anyways… here’s the physics discussion where I’m trying to find out how much granularity I can get with the new manual simulation of physics… could you help translate from MelvMay English to my moron designer language?

https://discussions.unity.com/t/698428

For the lookahead glitches, I would simplify the code, so that it does something like this:

Vector3 posDelta = teleportDest.transform.position - teleportOrigin.transform.position;
player.transorm.position += posDelta;
vcam.OnTargetObjectWarped(posDelta);

You don’t need a fast framerate or anything. If the player is moving really fast and you don’t get the trigger until it’s well past the portal, it won’t make any difference, you’ll be warped to a point similarly past the destination portal.