- Home /
Is it possible to keep the same Vector3 value using ScreenToWorldPoint with the camera moving?
I basically want an object/character to move to the mouse/touched position on the screen, with the camera following obviously, but the values keep updating so the player never reaches destination. I need the object to move to a set location, with the camera following, and no values updating until the next click or touch. Help is appreciated.
void Start()
{
curPos = gameObject.transform.position;
cc = GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetMouseButton(0))
{
if (arrived == false)
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
curPos = transform.position;
lastPos = target;
//destination = new Vector3(lastPos.x, lastPos.y, transform.position.z);
//transform.position = Vector3.MoveTowards(transform.position, lastPos, moveSpeed * Time.deltaTime);
if (lastPos == target)
{
//Camera.main.transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime) - new Vector3(1f, 1f, 0f);
if (curPos == lastPos)
{
arrived = true;
}
}
else
{
arrived = false;
}
}
transform.position = Vector3.MoveTowards(transform.position, lastPos, moveSpeed * Time.deltaTime);
}
else
{
moveDirection.y -= gravity * Time.deltaTime;
arrived = false;
}
cc.Move(moveDirection * Time.deltaTime);
if (cc.isGrounded == true)
{
moveDirection.y = 0f;
}
}
Comment
Your answer
Follow this Question
Related Questions
What is this error and how can i fix it? 1 Answer
Switching between multiple classes 2 Answers
[Unity Issue] CharacterController falls through rotating platform 1 Answer
character in Reverb Zone? 1 Answer
Realtime object loading to scene 1 Answer