- Home /
Object keeps snapping to camera size on play
I'm in the process of making my gameobject move when I drag it across the screen on touch. But my current issue is when I press play to test the game, game object snaps to the size of my camera on the Y. The camera by default is set to a size of "5". The game object will jump to the "5" position on play. I cannot figure out why. Instead of the object staying at the -4.5f like I have in the Vector3, it jumps to the camera size.
Here is what I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PaddlePosition : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
OnFingerDrag();
}
private void OnFingerDrag()
{
Vector3 fingerPosition = new Vector3(Input.mousePosition.x, -4.5f, 0.0f);
Vector3 objectPosition = Camera.main.ScreenToWorldPoint(fingerPosition);
transform.position = objectPosition;
}
}
Answer by highpockets · Jun 19, 2019 at 09:45 PM
You need to define a distance from the camera that you expect the plane at which you would like to translate the world point to as the z/3rd parameter of the Vector3 passed. Change the ScreenToWorldPoint() line to this:
Vector3 objectPosition = Camera.main.ScreenToWorldPoint(new Vector3(fingerPosition.x, fingerPosition.y, (transform.position - Camera.main.transform.position).magnitude));
Your answer
Follow this Question
Related Questions
Camera Movement 1 Answer
Rigidbody Moveposition not working when Camera follows object? 0 Answers
Camera Relative Movement 2 Answers
First person camera restriction. 1 Answer
How can I produce the typical FPS mouse steering effect? 2 Answers