Question by
BrioBlu · Nov 14, 2019 at 08:12 PM ·
c#unity 5movement scriptplayer movementdistance check
Help with Vector3.Distance
Hello guys. I'm trying to move the player object with mouse click using a player controller. my class is public class UNET_PlayerMouseController : NetworkBehaviour
public float speed;
public CharacterController playerController;
private Vector3 m_Position ;
void Start()
{
if (!isLocalPlayer)
return;
m_Position = transform.position;
}
void Update()
{
if (!isLocalPlayer)
return;
if (Input.GetMouseButton(0))
locatePosition();
moveToPosition();
}
void locatePosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000))
{
m_Position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
Debug.Log("New position " + m_Position);
}
}
void moveToPosition()
{
if (Vector3.Distance(transform.position, m_Position)>1)
{
Quaternion newRotation = Quaternion.LookRotation(m_Position - transform.position, Vector3.forward);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
playerController.SimpleMove(transform.forward * speed);
}
}
} After the mouse click, my object move to position correctly, but after he keeps going forward. I think the problem is that
Vector3.Distance(transform.position, m_Position)
doesn't return 0 when player move on the right position.
Is it possible???
Thank You.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612214820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The referenced script on the behaviour is missing error help plz 2 Answers
Move Player along normal of floor? 1 Answer
Let the Player face towards its move direction? 1 Answer
Can't jump when moving backwards and to the right or left (C#) 1 Answer
MoveTowards not moving accurately. Getting stuck on target. 2 Answers