Eric Zhang
☺
creative practice specializing in design & motion
CardVisual.cs
using UnityEngine;
public class CardVisual : MonoBehaviour
{
[Header("Target to Follow")]
public Transform targetTransform; // Drag your invisible target object here
[Header("Movement Speeds")]
[SerializeField] private float moveSpeed = 12f;
[SerializeField] private float rotationSpeed = 10f;
[Header("Juice & Tilt Settings")]
[SerializeField] private float tiltAmount = 0.15f;
[SerializeField] private float maxTilt = 15f;
private Vector3 lastPosition;
private Vector3 currentVelocity;
private void Start()
{
if (targetTransform != null)
{
transform.position = targetTransform.position;
lastPosition = transform.position;
}
}
private void Update()
{
if (targetTransform == null) return;
// 1. Calculate how fast the visual is moving across the screen
Vector3 targetPos = targetTransform.position;
currentVelocity = (targetPos - lastPosition) / Time.deltaTime;
lastPosition = transform.position;
// 2. Smoothly move (Lerp) towards the target position
transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * moveSpeed);
// 3. Tilt the card based on sideways movement speed
float zTilt = Mathf.Clamp(-currentVelocity.x * tiltAmount, -maxTilt, maxTilt);
Quaternion targetRotation = targetTransform.rotation * Quaternion.Euler(0, 0, zTilt);
// 4. Smoothly rotate towards the target tilt angle
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
}
}

