• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 Crazie_Siberian · Sep 12, 2016 at 06:16 AM · 2dglitchmouse look

How to fix mouse-look goes crazy when going too far?

So, I'm currently working on a 2D, top-down space game. The players ship looks at the mouse position. It all works fine, until the ship flies a certain distance (i tested it and when it reaches 80 on the Y axis is when it starts to flip out. didn't test on the X axis).

I'd really like some help with this. I have no idea why the steering starts to flip out.

Here's a link to a video of the issue, so you have a better idea of what's going on. VIDEO

Anyways, here's my script for the player controller.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     Vector2 moveDirection;
     Rigidbody2D shipBody;
     SpriteRenderer shipRenderer;
 
     public Sprite shipSprite;
 
     public float speed = 15.0f;
     public float sidewaysThrustMultiplier = 0.25f;
     public float turnSpeed = 15.0f;
     public bool isPowered = false;
     
 
     void Start ()
     {
         //Get the rigidbody of the player.
         shipBody = gameObject.GetComponent<Rigidbody2D>();
 
         //Get the sprite renderer of the ship.
         shipRenderer = gameObject.GetComponent<SpriteRenderer>();
 
         //Set the default ship sprite. [Will be removed when ship selection screen is implemented]
         shipRenderer.sprite = shipSprite;
     }
     
 
     void Update ()
     {
         //Make sure we can only do things while the ship is powered.
         if (isPowered)
         {
             //Get the direction we want the ship to move based on input.
             moveDirection = new Vector2(Input.GetAxis("Horizontal") * sidewaysThrustMultiplier, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
 
             //Look at mouse position.
             Vector3 mousePosition = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
             float angle = (Mathf.Atan2(mousePosition.y - transform.position.y, mousePosition.x - transform.position.x) * Mathf.Rad2Deg);
             transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle - 90), turnSpeed * Time.deltaTime);
         }
     }
 
     void FixedUpdate()
     {
         //Move ship in a direction based on the moveDirection variable.
         shipBody.AddForce(moveDirection);
     }
 }
 


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
Best Answer

Answer by Adarn · Sep 13, 2016 at 06:05 AM

It looks as tho your issue is with your angle calculation (Line 43). The problem is that your are mixing world and screen co ordinates in order to determine the angle between the mouse and the ship. The angle you want should be calculated by the ships position on screen not in world.

The transform values you are using in the angle calculation need to be converted to screen coordinates.

Why your ship spins? This is probably because in the beginning of your vid the world position of your ship (transform.position.y) is probably close enough to the value required to get the angle you want but as your ship moves through the world its going to change, and possible become negative and get further from what you need.

Comment
Add comment · Show 6 · 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 Crazie_Siberian · Sep 14, 2016 at 01:08 AM 0
Share

The angle you want should be calculated by the ships position on screen not in world.

Unity Documentation states that WorldToScreenPoint "Transforms position from world space into screen space." You say I should calculate the angle based on the ships position on the screen, which is exactly what WorldToScreenPoint does if I understand it correctly, right?

avatar image Adarn Crazie_Siberian · Sep 14, 2016 at 01:39 AM 0
Share

Yes you are correct. The problem is that this has not been done to ALL the values in your angle calculation.

In your calculation for mousePosition you convert the ships world position to its screen position. This gives you a vector of the distance from the ship to your mouse cursor in screen coordinates.

The problem is occurring in your angle calculation. In here you take mousePostion (screen coordinate) and subtract from it transform.position (this is world coordinate). It is in the calculation that you are missing the conversion to screen coordinates for the transform.position value. Without this conversion the value in transform.position is dependent on your ships location in the game world. This value will vary drastically during the course of the game. Once you convert it to a screen coordinate however it will be a fairly consistent value (given that your camera has a nice smooth follow effect on it so its not always at the screen centre).

To verify this check the value of transform.position at the start of your game. Then move the ship to the problem area and check it again.

avatar image Crazie_Siberian Adarn · Sep 14, 2016 at 01:51 AM 0
Share

Ok, i am now using the following code to determine my angle, and it's not turning out so well. The ship doesn't point to the mouse at all now, it just points some random direction.

 float angle = (Mathf.Atan2(mousePosition.y - Camera.main.WorldToScreenPoint(shipBody.gameObject.transform.position).y, mousePosition.x - Camera.main.WorldToScreenPoint(shipBody.gameObject.transform.position).x) * Mathf.Rad2Deg);

Show more comments
avatar image Crazie_Siberian · Sep 14, 2016 at 04:40 AM 0
Share

THANKS A LOT ADARN!

The final code for having the sprite look at the mouse position is:

 float angle = (Mathf.Atan2(Input.mousePosition.y - Camera.main.WorldToScreenPoint(gameObject.transform.position).y, Input.mousePosition.x - Camera.main.WorldToScreenPoint(gameObject.transform.position).x) * Mathf.Rad2Deg);
                 transform.localRotation = Quaternion.Slerp(transform.localRotation, Quaternion.Euler(0, 0, angle - 90), turnSpeed * Time.deltaTime);

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Player Look at Mouse Unity 4.3.1 2D 1 Answer

2D Animation does not start 0 Answers

How To Make An Invisible Wall That's Impossible To Get Out Of? 1 Answer

Object Changing Y Coordinate in Game View 0 Answers

weird glitch with 2d sprite fade with leantween 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges