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 /
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 ship 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 this 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 ShipAccelerates;
     public float ShipSpeed = 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 = ShipSpeed*Time.deltaTime;
         if (ShipAccelerates)
         {
             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 orthographic 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

would this work without a sprite because my spaceship is not a sprite. i will look into the code in a bit.

avatar image mnmwert · Jun 18, 2016 at 11:04 PM 0
Share

O$$anonymous$$G it works thank you so much!!

avatar image mnmwert · Jun 18, 2016 at 11:31 PM 0
Share

The only other thing I need to do is have the player point int the direction it is traveling in and tilt it when it turns!

avatar image mnmwert · Jun 19, 2016 at 08:01 PM 0
Share

How would I rotate it towards the mouse?

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

now the player moves backward wherever i press mouse0

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 this 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

Thank you for trying to help! there is another problem though. the player will just move up (a movement i don't want to go in since this is 2D top down)

avatar image TheMazinTar mnmwert · Jun 17, 2016 at 12:50 AM 0
Share

Okay, I'm a little confused.

From what perspective is your camera to the Unity world coordinates? When you said top down, I just assumed that you meant the style and not necessarily that you've literally positioned the camera and scene to be looking from the top down. There's nothing wrong with doing so, but is this right?

Even so, that's a weird problem . . . By up, you mean along the y-axis, right? Try setting the z-axis of the mouse position, then. Like this:

 Vector3 mousePosition = Input.mousePosition;
 mousePosition = new Vector3(mousePosition.x, $$anonymous$$athf.Abs(PlayerCamera.transform.position.y), mousePosition.z);
 transform.position = Vector3.$$anonymous$$oveTowards(transform.position, PlayerCamera.ScreenToWorldPoint(mousePosition), speed * Time.deltaTime);

The idea here is that the depth into the screen of the mouse (the z-axis that it doesn't automatically have because it's exists in 2D only) matches the distance between the camera and world. Does this at least fix the problem of the player moving up?

avatar image mnmwert · Jun 17, 2016 at 12:52 PM 0
Share

$$anonymous$$y camera is rotated so that it looks down on the player. When I said top down i meant literally. I am new at coding and i have recently started on my second game.. I have never used controls like this before so it is very new. By Up I do mean the Y axis in the inspector but for some reason I in my first game I remember it in the actual code being the Z axis in things like new Vector3(x,y,z). In answer to your question It does not fix the problem. I should say though that I have Unity 5.2 not 5.3.5 or 5.4.0. I will get 5.4.0 when the actual version of it comes out to help save my computer space. Thank you again for helping!

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

Use GameObject's global position instead of local position 3 Answers

How to reach limit line, keep moving, even past over the touch position? 0 Answers

Object following other object (Parent-like) 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