Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by Eyho · Mar 17, 2016 at 10:04 PM · camera2dmovementcamera-movement

2D Diablo style movement

Hello everybody!

I'm using the following script grabbed from here: http://wiki.unity3d.com/index.php/Click_To_Move_C This basically gives a very nice click to move control to my game, but I've got a problem with it. I'm making a 2D game for mobile, and for some reason it doesn't work if I try to use it in a "2D environment" (I tried the script with perspective camera in 3d and it worked!).

I've been struggling for hours and since I'm beginner I don't know how to modify this script to make it work. I basically have an ortographic camera, and a sprite on my scene which I attached the script onto. What else do I miss?

Thank you in advance!!!

Comment
Add comment · Show 2
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
avatar image MechanicalGaming · Mar 17, 2016 at 10:08 PM 0
Share

It might be because of the vector3s in the script but i'm not 100% sure about that one. It might be those because a 2d game doesn't have any depth only x and y (or z).

avatar image Cherno · Mar 18, 2016 at 01:44 AM 0
Share

2d uses the x and y axis. In a top-down game, x would be west-east and y would be north.south. The z axis is only used for things like Raycasting. Why is that important? Because the click to move script is indeed set up for a 3d environment, which you already know... And now you also will see how this is a problem. In a 3d environment, x is west-east as in 2d, but y is up-down and z is north-south. What you have to do is change the values assigned to the Vector3 variables like transform.position so they reflect the 2d environment.

It would also help if you posted what happens when you use the script in your 2d scene, ins$$anonymous$$d of just saying that it doesn't work :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by g2thet · Aug 09, 2017 at 06:42 AM

Try using this script as your player controller. This works in my 2d game. I found it in the following tutorial Click To Move in Unity 5. The example he creates in the tut is for 3d but it worked for my 2d game. Good luck and hope this helps!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [DisallowMultipleComponent]
 public class ClickToMove : MonoBehaviour 
 {
 
     [SerializeField] [Range(1,20)]
     private float moveSpeed = 5;
 
     private Vector3 targetPosition;
     private bool isMoving;
 
 
     // Use this for initialization
     void Start () 
     {
         targetPosition = transform.position;
         isMoving = false;
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetMouseButton(0))
         {
             SetTargetPosition ();
         }
 
         if(isMoving)
         {
             MovePlayer ();
         }
 
     }
 
     void SetTargetPosition()
     {
         targetPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         targetPosition.z = transform.position.z;
         isMoving = true;
     }
 
     void MovePlayer()
     {
         transform.rotation = Quaternion.LookRotation (Vector3.forward, targetPosition - transform.position);
         transform.position = Vector3.MoveTowards (transform.position, targetPosition, moveSpeed * Time.deltaTime);
         if(transform.position == targetPosition)
         {
             isMoving = false;
         }
 
     }
 }
 


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

6 People are following this question.

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

Related Questions

[2D] Camera movement causes flickering/jittering sprites 2 Answers

Regarding transform.position in the roll a ball tutorial 1 Answer

Jittery Movement - specific mechanics in mind 0 Answers

Help with a 2d camera Controller 1 Answer

Top down movement and rotation 2 Answers


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