• 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 /
This question was closed Jun 27, 2016 at 06:07 PM by mnmwert for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mnmwert · Jun 13, 2016 at 03:41 PM · positiondirectionplayer movementfollowmouseposition

Make player follow mouse position

I have a space s$$anonymous$$p in a 2D top down game. Ive been messing around with a script from the unity answers. Here is the script:

using UnityEngine; using System.Collections;

public class playerController : MonoBehaviour { public float speed = 1.5f; private Vector3 target; public Camera PlayerCamera;

 void Start()
 {
     target = transform.position;
 }

 void Update()
 {
     if (Input.GetKey(KeyCode.Mouse0))
     {
         target = PlayerCamera.ScreenToWorldPoint(Input.mousePosition);
         target.y = transform.position.y;
         target = new Vector3(target.y, transform.position.x, target.z);
         transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
     }
     //transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
 }

}

when I click the mouse0 button my player moves a little to the left and stops. I do not know why t$$anonymous$$s is. At the moment I am just trying to get it to follow the mouse but eventually the player will rotate to go a different direction when the mouse position is changed.

Thank you for your help!

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

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by aybeone · Jun 17, 2016 at 04:02 PM

Here's a simple approach that can also move smoothly:

 using System;
 using UnityEngine;
 
 public class NewBehaviourScript1 : MonoBehaviour
 {
     private Vector3 _target;
     public Camera Camera;
     public bool FollowMouse;
     public bool S$$anonymous$$pAccelerates;
     public float S$$anonymous$$pSpeed = 2.0f;
 
     public void OnEnable()
     {
         if (Camera == null)
         {
             throw new InvalidOperationException("Camera not set");
         }
     }
 
     public void Update()
     {
         if (FollowMouse || Input.GetMouseButton(0))
         {
             _target = Camera.ScreenToWorldPoint(Input.mousePosition);
             _target.z = 0;
         }
 
         var delta = S$$anonymous$$pSpeed*Time.deltaTime;
         if (S$$anonymous$$pAccelerates)
         {
             delta *= Vector3.Distance(transform.position, _target);
         }
 
         transform.position = Vector3.MoveTowards(transform.position, _target, delta);
     }
 }

(I did the following setup: added a sprite object and that script to it, then modified main camera to an orthograp$$anonymous$$c projection.)

Comment
Add comment · Show 4 · 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 mnmwert · Jun 18, 2016 at 07:59 PM 0
Share
avatar image mnmwert · Jun 18, 2016 at 11:04 PM 0
Share
avatar image mnmwert · Jun 18, 2016 at 11:31 PM 0
Share
avatar image mnmwert · Jun 19, 2016 at 08:01 PM 0
Share
avatar image
0

Answer by Simplified · Jun 13, 2016 at 04:48 PM

Try changing Update to FixedUpdate.

Comment
Add comment · Show 1 · 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 mnmwert · Jun 14, 2016 at 05:49 PM 0
Share
avatar image
0

Answer by TheMazinTar · Jun 14, 2016 at 07:18 PM

So, you just want the object to follow the mouse? If that's the case, I'm pretty sure that you only need two of the four lines of movement code that you have.

Try using just these two lines:

      target = PlayerCamera.ScreenToWorldPoint(Input.mousePosition);
      transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);

Or, if you wanted, you could even simplify it further and just go:

      transform.position = Vector3.MoveTowards(transform.position, PlayerCamera.ScreenToWorldPoint(Input.mousePosition), speed * Time.deltaTime);

Hope t$$anonymous$$s helps!

Comment
Add comment · Show 3 · 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 mnmwert · Jun 16, 2016 at 07:33 PM 0
Share
avatar image TheMazinTar mnmwert · Jun 17, 2016 at 12:50 AM 0
Share
avatar image mnmwert · Jun 17, 2016 at 12:52 PM 0
Share

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

46 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

Related Questions

Camera rotation around player while following. 6 Answers

Check mouse position relative to the player position 1 Answer

Object following other object (Parent-like) 2 Answers

How to make an object orient to the mouse direction 0 Answers

how to have a sprite follow the spot of my mouse cursor. 0 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