I’m trying to get a capsule (named “chars[0]” below) to move when I click, and stop when it reaches the target. The target is a sphere that follows your mouse, but when the capsule reaches it, it doesn’t stop.
Here is my code:
{
public Vector3 placement;
public Vector3 charsPlacement;
public GameObject[] chars;
public GameObject self;
public moving otherBool;
// Start is called before the first frame update
void Start()
{
placement.y = 1f;
Cursor.lockState = CursorLockMode.Locked;
placement.x = 0f;
placement.z = 0f;
GameObject g = GameObject.FindGameObjectWithTag("ActiveCharacter");
otherBool = g.GetComponent<moving>();
}
// Update is called once per frame
void Update()
{
placement.x += Input.GetAxis("Mouse X");
placement.z += Input.GetAxis("Mouse Y");
transform.position = placement;
if (Input.GetMouseButtonDown(0))
{
placement.y = 1f;
chars[0].transform.LookAt(self.transform.position);
otherBool.move = true;
Cursor.lockState = CursorLockMode.None;
}
if (transform.position == chars[0].transform.position)
{
otherBool.move = false;
}
}
}
The code for the capsule:
{
public GameObject self;
public GameObject target;
public Vector3 targetLocal;
public bool move = false;
public float speed = 1f;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (move)
{
speed = 5f;
self.transform.position += transform.forward * Time.deltaTime* speed;
}
if(!move)
{
speed = 0f;
}
}
}
(Im sorry im still kind of new, so Im not sure what’s wrong so I added it all)
Does anyone have any ideas?