- Home /
Hotline Miami style camera!
I wanted to have Hotline Miami style camera in my game. I found pretty decent tutorial how to code it in GameMaker and I thought that I could implement it in Unity- but I don't know how. Could anyone help me? Video: Tutorial
That's orginall GML code from tutorial:
var xTo,yTo;
move_towards_point(mouse_x,mouse_y,0);
xTo = obj_player.x + lengthdir_x(min(96,distance_to_point(mouse_x,mouse_y)),direction);
yTo = obj_player.y + lengthdir_y(min(96,distance_to_point(mouse_x,mouse_y)),direction);
x += (xTo-x)/25;
y += (xTo-y)/25;
view_xview = -(view_wview/2) + x;
view_yview = -(view_hview/2) + y;
And that's my kinda working reimplementation:
using UnityEngine;
using System.Collections;
public class SmoothCamera : MonoBehaviour {
public Camera PlayerCam;
public Transform myPlayerPos;
Vector3 newCamPos;
float xTo,yTo;
void Update () {
var mouse = Input.mousePosition; //Pozycja myszki
var screenPoint = PlayerCam.WorldToScreenPoint(myPlayerPos.transform.localPosition); //Pozycja pixelowa gracza
var offset = new Vector2(mouse.x - screenPoint.x, mouse.y - screenPoint.y); //Wektor gracz-myszka
float dist = Vector2.Distance(myPlayerPos.position, Input.mousePosition);
Debug.Log (dist.ToString("F4"));
xTo = myPlayerPos.transform.position.x + lengthdir_x (Mathf.Min(3, dist), offset);
yTo = myPlayerPos.transform.position.x + lengthdir_x (Mathf.Min(3, dist), offset);
newCamPos.x = xTo;
newCamPos.y = yTo;
newCamPos.z = -100;
transform.position = newCamPos;
}
static float lengthdir_x(float len, Vector2 dir){
dir = dir.normalized * len;
return dir.x;
}
static float lengthdir_y(float len, Vector2 dir){
dir = dir.normalized * len;
return dir.y;
}
}
My camera is a child of player sprite. It seems to be working but only when camera is moving from left bottom to right top corrner with werid shaking when centered on the player. Why is that?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612044339im_/https://answers.unity.com/themes/thub/images/avi.jpg)