Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Czar-Man · Nov 09, 2016 at 08:31 AM · raycast2d physicsclick to movepoint-and-clickmouse movement

2D Point & Click Movement not moving

I'm trying to make my character move to virtually any spot in an area that I click on. I'm currently using code and assets from the 2D Roguelike tutorial. I have Floor tile prefabs all containing a 2DBoxCollider set as Trigger.

I have also been given code that is supposed to check for the mouse's position, draws a raycast and starts moving the character to that position with lerping. MouseMovement script(attached to MainCamera) public class MouseMovement : MonoBehaviour { public GameObject Movechild = null; LerpToPoint Test = null;

 // Use this for initialization
 void Start ()
 {
     if(Movechild)
     {
         Test = Movechild.GetComponent<LerpToPoint>();
     }
 }
 
 // Update is called once per frame
     void Update ()
     {
 
         if (Input.GetButtonUp("LMB"))
         {
             //var vMoveLocation = Input.mousePosition;
             var vMoveLocation = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Ray ray = Camera.main.ScreenPointToRay(vMoveLocation);
             RaycastHit hit = new RaycastHit();
 
             //ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             print("Mouse: " + vMoveLocation);
             //if (Physics.Raycast(ray, out hit))
             //if (Physics.Raycast(vMoveLocation, out hit, Vector3.forward, 100))
             //if (Physics.Raycast(vMoveLocation, out hit, Vector3.forward, 100))
             //if (Physics.Raycast(vMoveLocation, Vector3.forward, out hit))
 
             if (Physics.Raycast(ray, out hit))
             {
                 //Debug.Log(hit.collider.name);
                 print("Clicked");
 
                 GameObject.Instantiate(Resources.Load("testSprite"), hit.point, Quaternion.identity);
 
                 Test.EndPos = hit.point;
                 Test.Activate();
             }
             else
             {
                 print("Point: " + hit.point);
             }
         }
     }

}

LerpToPoint(attached to Player), which moves the character to the area clicked.

 public class LerpToPoint : MonoBehaviour 
 {
   public Vector3 EndPos   = new Vector3(0,0,0);
   Vector3 StartPos = new Vector3(0, 0, 0);
   bool Active = false;
         
   private float startTime;
   public float Duration = 0.05f;
 
   void Start()
   {
     StartPos = transform.position;
   }
 
 
   public void Activate()
   {
     Active = true;
     startTime = Time.time;
     StartPos = transform.position;
   }
 
     // Update is called once per frame
     void Update () 
     {
         if(Active == true)
         {
           float distTravelled = (Time.time - startTime) / Duration;
           float fracJourney = Time.time - startTime;
       
           transform.position = Vector3.Lerp(StartPos, EndPos, distTravelled);
     
           if(fracJourney >= Duration)
           {
             Active = false;
           }
         }
     }
 }
 

I've tried different combinations of code for Raycasting, but I can't seem to get it to move no matter what I do. What is a good way to go about this?

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
1

Answer by UnityTrouble · Dec 27, 2016 at 10:39 PM

Hi, we made a 2d/2.5d point and click and it is now available on the asset store at https://www.assetstore.unity3d.com/en/#!/content/76020

It might help you with your game. We also take requests of code so let us know what you need and we might be able to add it to the asset store.

holwerda.inc@gmail.com hwaus.com

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Leave a mark on clicking point of click to move 1 Answer

Player won't move to a certain location clicked by the mouse 0 Answers

raycast navmesh click to move 1 Answer

Setting navmesh destination by clicking on minimap 1 Answer

Raycast won't hit Collider 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