gpt-4-0314· OpenAI
WildChat-4.8M (AI2)
I need a Unity function that will let me move camera smoothly between two positions with…
- User
I need a Unity function that will let me move camera smoothly between two positions with damping and fixed time of movement no matter the distance between those two points
- Assistant
Here's a simple Unity script to smoothly move the camera between two positions with damping and fixed time of movement.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraMover : MonoBehaviour { public Vector3 startPosition; public Vector3 endPosition; public float damping = 0.1f; public float fixedMoveTime = 2f; // seconds private float startTime; private bool isMoving = false; private Camera mainCamera; void Start() { mainCamera = Camera.main; } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { StartMoving(); } if (isMoving) { MoveCamera(); } } void StartMoving() { startTime = Time.time; isMoving = true; } void MoveCamera() { float t = (Time.time - startTime) / fixedMoveTime; if (t < 1f) { Vector3 newPosition = Vector3.Lerp(startPosition, endPosition, t * (2 - t) * damping); mainCamera.transform.position = newPosition; } else { // After reaching the destination isMoving = false; mainCamera.transform.position = endPosition; } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.