How do i move the game object towards the mouse clicked point smoothly?
Hello guys, I am new in unity development(3 months) and I try to improve myself by developing games. I want to move my gameObject to mouse click position. You can see my problem in video above. The player move to clicked direction but it's not complete the distance. When I click the position the player move on the right direction but if the mouse click position far, it can move only half of distance or maybe less.When its on the same x axis it move right but in cross movement it can't reach the position with one click.
Thats the player's script. I want to move the player to clicked point smoothly so I'm waiting for your help, thanks in advance. İf I increase the moveSpeed on the code below it can reach the point but ıt's too fast so ı don't want to do this.
public class UnitRTS : MonoBehaviour
{
public bool isSelected;
public GameObject select;
public float moveSpeed = 50f;
private Rigidbody rb;
Vector3 newPosition;
private Animator anim;
Vector3 targetPosition;
Vector3 pos;
private Enemy enemy;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
isSelected = false;
newPosition = transform.position;
anim = GetComponent<Animator>();
targetPosition = transform.position;
enemy = GameObject.FindWithTag("Enemy").GetComponent<Enemy>();
}
private void FixedUpdate()
{
}
// Update is called once per frame
void Update()
{
Selected();
LookTowardMouse();
Move();
}
void Selected()
{
if (isSelected)
{
select.gameObject.SetActive(true);
}
else
{
select.gameObject.SetActive(false);
}
}
void LookTowardMouse()
{
if (isSelected)
{
//Get the Screen positions of the object
Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
//Get the Screen position of the mouse
Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
//Get the angle between the points
Vector3 a = positionOnScreen;
Vector3 b = mouseOnScreen;
float angle = Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg; //AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
//Ta Daaa
transform.rotation = Quaternion.Euler(new Vector3(0f, -angle - 65f, 0f));
}
}
public void Move()
{
if (isSelected && Input.GetMouseButtonUp(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
targetPosition = hit.point;
Vector3 dir = (targetPosition - transform.position).normalized;
transform.position += dir * Time.deltaTime * moveSpeed;
Vector3 temp = transform.position;
temp.y = 0.4f;
transform.position = temp;
//anim.SetBool("isWalking", true);
anim.SetBool("isCharge", true);
}
}
else
{
//anim.SetBool("isWalking", false);
anim.SetBool("isCharge", false);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
anim.SetBool("isAttack", true);
enemy.isAngry = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Enemy")
{
anim.SetBool("isAttack", false);
}
}
}
I would like to point out that there may be people who haven't seen it. Still waiting for your help.
Your answer
Follow this Question
Related Questions
impact mouseclick 0 Answers
OnMouseDown function not working on a gameObject 1 Answer
How do I make a rigid body move towards position of touch 0 Answers
How to make spawned in game objects pause with mouse click. 1 Answer
Move object to another position after Mouse Click with MoveTowards function 1 Answer