- Home /
How can i make my crosshair drag behind the camera?
Hello, i am trying to make my crosshairdot drag behind my camera. For example when i turn right, i want the crosshair to be slower and drag behind to the left of the center of the screen, after which it should correct again to the center.
The code below is my first attempt but the Camera.main.transform.forward doesnt seem reliable in determining the direction for the dot. The current code sometimes makes the dot go left when the camera goes left fast enough.
Any help is appreciated.
using System.Collections;
using UnityEngine;
public class Crosshair_Dot_Mover : MonoBehaviour {
public GameObject crosshairDot;
float distance = 1;
double standardX = 142;
double standardY = 159.5;
public float dotVectorX;
public float dotVectorY;
void Update () {
dotVectorX = crosshairDot.transform.position.x;
dotVectorY = crosshairDot.transform.position.y;
StartCoroutine(DragDot());
StartCoroutine(PositionToZero());
}
IEnumerator DragDot() {
yield return new WaitForSeconds(0.1f);
if ((dotVectorX > 130 && dotVectorX < 155) && (dotVectorY > 147 && dotVectorY < 172)) {
transform.position = transform.position + (Camera.main.transform.forward * -1) * distance / 2;
}
}
IEnumerator PositionToZero() {
yield return new WaitForSeconds(0.1f);
if (dotVectorX >= standardX) {
transform.position += new Vector3(-0.1f, 0, 0);
} else if (dotVectorX <= standardX) {
transform.position += new Vector3(0.1f, 0, 0);
}
if (dotVectorY >= standardY) {
transform.position += new Vector3(0, -0.1f, 0);
} else if (dotVectorY <= standardY) {
transform.position += new Vector3(0, 0.1f, 0);
}
}
}
why don't you attach it with some physics joint ins$$anonymous$$d of hardcoding this behaviour?
Your answer
Follow this Question
Related Questions
UI objects move at different speeds on different resolutions 1 Answer
How do i lock the position of the camera above the player relative to the origin point? 0 Answers
Transform an object based on another object's velocity? 0 Answers
Imparting Physics in only certain planes of motion 1 Answer
Move to touch position 1 Answer