Answer the question
In order to leave comments, you need to log in
Why doesn't the player's position change?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class circle : MonoBehaviour
{
bool MouseDown = false;
Vector3 posPlayer;
void Start()
{
posPlayer = GetComponent<Transform>().position;
}
void onMouseDown()
{
MouseDown = true;
}
void onMouseUp()
{
MouseDown = false;
}
void Update()
{
Vector2 Cursor = Input.mousePosition;
Cursor = Camera.main.ScreenToWorldPoint(Cursor);
if(MouseDown)
{
posPlayer = Cursor;
}
}
}
Answer the question
In order to leave comments, you need to log in
position is not a reference type.
Do this:
posPlayer.position = Cursor
and so, if the LMB is pressed, then the cursor coordinates are equal to a variable that stores 3 values (not the current coordinates, but the values).
If the left-click is not pressed, then the cursor is created , after which, it is set to Input.mousePosition, which is the deviation from the screen angle in pixels). After that, this deviation is converted into coordinates.
Vector2 Cursor = Input.mousePosition;
Cursor = Camera.main.ScreenToWorldPoint(Cursor);
if(MouseDown)
{
posPlayer = Cursor;
}
//логичней будет сделать так
public Vector2 Cursor;//публичные переменные принято писать с заглавной буквы
void Update()
{
if(MouseDown)
{
posPlayer = Camera.main.ScreenToWorldPoint( Input.mousePosition);
}
}
Cursor is a unity class. I do not think that the error is due to this, but it seems to me that it is better to name the variable differently.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question