Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed May 01, 2021 at 04:46 PM by Hibinger for the following reason:

The question is answered

avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

255 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help converting a character controller script to rigidbody 1 Answer

rigidbody movement is not smooth 0 Answers

rb. MovePosition slowly enters another Collider 0 Answers

Need help with high speed collisions, sanity dwindling 1 Answer

Face direction of a Vector 3 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges