0 camera drag-and-drop unity-game-engine smooth-scrolling easing
I\xc2\xb4m 尝试在拖动相机时进行缓动或惯性,以便当我放下相机时它会缓动到位。我想根据我抛出/拖动相机的力来移动相机。
\n这是 i\xc2\xb4m 用于拖动相机的实际代码,但其中没有平滑的缓动。
\n using UnityEngine;\n using System.Collections;\n\n public class swipeCamera: MonoBehaviour\n {\n Vector3 hit_position = Vector3.zero;\n Vector3 current_position = Vector3.zero;\n Vector3 camera_position = Vector3.zero;\n float z = 0.0f;\n\n // Use this for initialization\n void Start()\n {\n }\n\n void Update()\n {\n if (Input.GetMouseButtonDown(0))\n {\n hit_position = Input.mousePosition;\n camera_position = transform.position;\n\n }\n if (Input.GetMouseButton(0))\n {\n current_position = Input.mousePosition;\n LeftMouseDrag();\n }\n }\n\n void LeftMouseDrag()\n {\n // From the Unity3D docs: "The z position is in world units from the camera." In my case I\'m using the y-axis as height\n // with my camera facing back down the y-axis. You can ignore this when the camera is orthograhic.\n current_position.z = hit_position.z = camera_position.y;\n\n // Get direction of movement. (Note: Don\'t normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)\n // anyways. \n Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);\n\n // Invert direction to that terrain appears to move with the mouse.\n direction = direction * -1;\n\n Vector3 position = camera_position + direction;\n\n transform.position = position;\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n谢谢你!
\n放松意味着随着时间的推移逐渐移动。
因此,此类行为通常使用Vector.MoveTowards或其他 lerp 函数以及方法中的Time.deltaTimeUpdate来实现。
using UnityEngine;
using System.Collections;
public class swipeCamera: MonoBehaviour
{
public float speed = 2.0f;//easing speed
Vector3 hit_position = Vector3.zero;
Vector3 current_position = Vector3.zero;
Vector3 camera_position = Vector3.zero;
float z = 0.0f;
bool flag = false;
Vector3 target_position;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
hit_position = Input.mousePosition;
camera_position = transform.position;
}
if (Input.GetMouseButton(0))
{
current_position = Input.mousePosition;
LeftMouseDrag();
flag = true;
}
if(flag)
{
transform.position = Vector3.MoveTowards(transform.position, target_position, Time.deltaTime*speed);
if(transform.position == target_position)//reached?
{
flag = false;// stop moving
}
}
}
void LeftMouseDrag()
{
// From the Unity3D docs: "The z position is in world units from the camera." In my case I'm using the y-axis as height
// with my camera facing back down the y-axis. You can ignore this when the camera is orthograhic.
current_position.z = hit_position.z = camera_position.y;
// Get direction of movement. (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
// anyways.
Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
// Invert direction to that terrain appears to move with the mouse.
direction = direction * -1;
target_position = camera_position + direction;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8447 次 |
| 最近记录: |