This question was 
             closed May 01, 2021 at 04:46 PM by 
             Hibinger for the following reason: 
             
 
            The question is answered
 
               Question by 
               Hibinger · Apr 29, 2021 at 11:22 AM · 
                movementrigidbodymovement scriptmouseclickmouse position  
              
 
              Move to mouse position, (if start pressing from the character) - HELP
I'm doing a small class project, where I need to make a character move to the target position pressed by applying a force. I got everything right so far after digging many videos and chats here, yet I cant make this only happen when the press starts from the character, and I'm not finding much information regarding that, any help is welcome!
THIS IS THE CODE
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MouseController : MonoBehaviour {
 private Rigidbody rb;
 private Vector3 targetPosition;
 private Vector3 lookAtTarget;
 private Vector3 direction;
 private Quaternion playerRot;
 private Animator anim;
 public GameObject point;
 public float rotSpeed;
 public float speed;
 private bool moving = false;
 // Start is called before the first frame update
 void Start()
 {
     rb = GetComponent<Rigidbody>();
     anim = GetComponent<Animator>();
 }
 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButton(0))
     {
         //hitCollider != null && hitCollider.CompareTag ("Player")
         SetTargetPosition();
         Debug.Log(targetPosition);
         anim.SetBool("IsRunning", true);
     }
     /*if (!Input.GetMouseButton(0) && targetPosition == rb.position)
     {
         anim.SetBool("IsRunning", false);
     }*/
     if(moving == true)
     {
         Move();
     }
 }
 void SetTargetPosition()
 {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit hit;
     if(Physics.Raycast(ray, out hit, 10000))
     {
         if(hit.collider.CompareTag("Player"))
         {
             //Debug.Log ("This is a Player")
         }
         targetPosition = hit.point;
         //this.transform.LookAt(targetPosition);
         lookAtTarget = new Vector3(targetPosition.x - transform.position.x, transform.position.y, targetPosition.z - transform.position.z);
         playerRot = Quaternion.LookRotation(lookAtTarget);
         moving = true;
     }
 }
 void Move()
 {
     // Smooth look at
     transform.rotation = Quaternion.Slerp(transform.rotation, playerRot, rotSpeed * Time.deltaTime);
     //move with transform
     //transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
     direction = (targetPosition - transform.position).normalized;
     rb.velocity = new Vector3(direction.x, 0, direction.z) * speed * Time.deltaTime;
     if (Vector3.Distance(rb.transform.position, targetPosition) <= .5f)
     {
         moving = false;
         anim.SetBool("IsRunning", false);
     }
 }
 
               }
               Comment