• 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 KatieN · Feb 02, 2019 at 11:14 PM · c#2dmovementnot workingclick to move

Player moves to the middle of the room when the level starts (2d point and click)

Hi! I am new to Unity and I found this script online for moving the player using mouse input. But every time the level starts the player moves to the center of the scene. I'm not sure how to keep it where I placed it during setup. Please help! (Unity 2018.2.7f1, 2D) My code:

 public class MovePlayer2 : MonoBehaviour {
 
     public Vector2 targetPosition;
     [SerializeField]
     private Rigidbody2D rb;
     [SerializeField]
     private float speed = 10;
     // Update is called once per frame
     void FixedUpdate () {
  
         if(Input.GetMouseButtonDown(0)) { //checks that the left mouse button has been pressed
             targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); //sets the targetPosition to the dimensional point in the game where the mouse clicked
 
         }
         transform.position = Vector2.MoveTowards(transform.position, targetPosition, Time.deltaTime * speed); //it will move from the current position to the target position and the time.deltaTime 
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sean244 · Feb 03, 2019 at 01:15 AM

Just re-write your code to the following:

 using UnityEngine;
 
 public class MovePlayer2 : MonoBehaviour
 {
 
     public Vector2 targetPosition;
 
     private Rigidbody2D rb;
 
     [SerializeField]
     private float speed = 10;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody2D>();
     }
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         }
     }
 
     private void FixedUpdate()
     {
         if (targetPosition.magnitude > 0)
         {
             var position = Vector2.MoveTowards(transform.position, targetPosition, Time.deltaTime * speed);
             rb.MovePosition(position);
         }
     }   
 }
 
Comment
Add comment · Show 2 · 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
avatar image KatieN · Feb 03, 2019 at 02:33 AM 0
Share

Thank you!

avatar image sean244 KatieN · Feb 03, 2019 at 02:47 AM 0
Share

You’re welcome. Please subscribe to my YouTube channel https://www.youtube.com/channel/UCo_3O$$anonymous$$EZQiRLQihycbkYd_Q

avatar image
-1

Answer by CBlockSurprise · Feb 02, 2019 at 11:22 PM

You need to set the transform of the player's to an empty gameObject

 [SerializeField]
 private Transform spawn;
 
 void Start () {
    transform.position = spawn.position;
 }
Comment
Add comment · Show 5 · 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
avatar image CBlockSurprise · Feb 02, 2019 at 11:26 PM 0
Share

This makes it easier for having many scenes.

avatar image KatieN · Feb 02, 2019 at 11:42 PM 0
Share

So 1. make an empty gameObject. 2. make it the parent of the player. 3. Add this code. ?

avatar image CBlockSurprise KatieN · Feb 02, 2019 at 11:51 PM 0
Share
  1. $$anonymous$$ake an empty gameObject, then position it where you need the player to start out. Add the code and set the private transform to the empty gameObject.

avatar image CBlockSurprise CBlockSurprise · Feb 02, 2019 at 11:51 PM 0
Share

Don't parent it, or the spawn point will move with the player

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

238 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Question on Sprite and Movement 0 Answers

Better way to move avatar than transform.position? 1 Answer

Moving in circles 1 Answer

Simple C# script to move a gameObject toward the direction of the player at the instance of it's spawning. 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