Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by JordanLB · Jul 09, 2013 at 12:41 PM · mouseclickmousemove

Mouse2Move Not Working?

Hello, I'm trying to get this point and click script working. In my TP_MoveByMouse.cs I have the following code

 using UnityEngine;
 
 public class TP_MoveByMouse
 {
     private static TP_MoveByMouse singelton = null;
 
     private Vector3 movePoint = new Vector3(0,0.9f,0);
     private float distance = 0;
 
     private TP_MoveByMouse()
     {
     }
 
     public static TP_MoveByMouse GetInstance()
     {
         if(singelton != null)
         {
             return singelton;
         }
         var x = new TP_MoveByMouse();
         return x;
     }
 
     public void MoveByMouseKlick()
     {
         var player = GameObject.Find("Capsule");
         var playerCurrentPos = player.transform.position;
 
         if (Input.GetMouseButton(0))
         {
             movePoint = RayCastLineTest(playerCurrentPos);
             GetDistance(player);
             playerCurrentPos = player.transform.position;
 
             if (!player)
                 return;
         }
 
         if(distance > 0.1f)
         {
             var speed = 10f;
             var moveSpeed = speed * Time.deltaTime;
             var moveStep = movePoint - player.transform.position;
             moveStep.Normalize();
             
             player.transform.LookAt(movePoint);
             player.transform.position = playerCurrentPos + (moveStep * moveSpeed);
             playerCurrentPos = player.transform.position;
 
             GetDistance(player);
         }
     }
 
     private void GetDistance(GameObject player)
     {
         distance = Vector3.Distance(player.transform.position, movePoint);
         Debug.Log(string.Format("Distance: {0}", distance));
     }
 
     public Vector3 RayCastLineTest(Vector3 playerLoc)
     {
         var hit = new RaycastHit();
         Ray ray = (Camera.main.ScreenPointToRay(Input.mousePosition)  );
 
         
             Physics.Raycast(ray, out hit);
 
         Debug.DrawLine(Camera.main.transform.position, hit.point);
         
         return hit.transform.tag == "CanMoveOn" ? new Vector3(hit.point.x,hit.point.y + 0.9f,hit.point.z) : playerLoc;
     }
 }

And I'm calling it and instantiating it in controller_2.cs. It's attached to a capsule. Every time it runs I the debug distance shows "0 distance" and wont move. Am I calling it wrong?

controller_2.cs

 using UnityEngine;
 using System.Collections;
 
 public class controller_2 : MonoBehaviour {
     
     TP_MoveByMouse move = TP_MoveByMouse.GetInstance();
     
     
     // Update is called once per frame
     void Update () 
     {
         move.MoveByMouseKlick();
     
     }
 }

Any help would be greatly appreciated!!!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Jul 09, 2013 at 01:10 PM

what is raycastlinetest. Need to see the code also dont move like that move using lerp.

void update() { if (distance > .1) { player.transform.position = vector3.MoveTowards(player.transform.position,movepoint, speed * time.deltatime); } } that will move smoothly towards the target.

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Why does this not detect mouse movement? 1 Answer

Trying to move a instantiate gameobject with mousedown 1 Answer

Imported skehup into unity not moving as I want!!! 2 Answers

How Do Change This Code to X-Y ? 1 Answer

button.Select() and alternatives don't seem to work when called via new input system 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