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 /
avatar image
0
Question by Rocketman_Dan · Aug 17, 2016 at 10:01 PM · 2dtranslatevector2topdowntranslation

Translation in 2D with a normalised vector.

I really can't see why this doesn't do anything. The player character doesn't move at all but the rest of the script works fine. It doesn't throw any errors and the rest operates as intended. I know there are other ways involving setting the transform.position directly but I the problem with that is that moving diagonally end up faster than moving straight because the it produces a vector (in practice but not the data type) or 5x by 5y (i.e. 50^1/2 on the diagonal (about 7) rather than the 5 I want). Instead I want to produce a vector between x and y and normalise it before multiplying by 5 so that the max speed is consistent in all directions.

Here is my code:

         moveDirection = new Vector2 (Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
         moveDirection = moveDirection.normalized;
         transform.Translate(moveDirection * Time.deltaTime * maxSpeed);

Note I have also tried

 moveDirection.Normalize;

Which also compiled but didn't do anything.

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

Answer by DiegoSLTS · Aug 17, 2016 at 10:10 PM

With that little code I can only guess that maxSpeed is 0. Or you set the timeScale to 0 so Time.deltaTime is 0.

Add a Debug.Log line or a breakpoint on the debugger to check the values of moveDirection, Time.deltaTime, maxSpeed and the product of all three just before the Translate line.

Also, "moveDirection.Normalize;" should give you a compile error since you're missing the "()" before the ;.

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 Bunny83 · Aug 17, 2016 at 11:21 PM 1
Share

The other reasons that are also possible are:

  • $$anonymous$$aybe the Horizontal and / or Vertical axis have been changed in the input manager. So they might return 0 all the time because they aren't mapped to "any" / "the wrong" keys.

  • $$anonymous$$aybe this code fragment isn't inside Update.

  • $$anonymous$$aybe "Update" has been spellt wrong? Like "update" for example (common Java programmer error ^^).

  • $$anonymous$$aybe the script is disabled (checkbox).

ps: normalizing the vector has an often unwanted sideeffect. The input axis don't necessarily be 1 as soon as the user presses the button down (see the Sensitivity / Gravity settings of the axis in the input manager). normalizing will force you to full speed as soon as the input axis differ from "0". If the axis "gravity" isn't too high (and snap is off) if would also take longer to stop moving after the user released the keys.

Usually it's better to use Vector3.Clamp$$anonymous$$agnitude. This allows the vector to get any possible value as long as it's length is below the clamp limit. If it's larger it will be clamped to the given length:

      moveDirection = new Vector2 (Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
      moveDirection = Vector2.Clamp$$anonymous$$agnitude(moveDirection, 1f);
      transform.Translate(moveDirection * Time.deltaTime * maxSpeed);

This will ensure that the length of moveDirection never gets larger than 1f but it can get smaller. Controller and joystick users will love you ^^ (if the axis are setup).

Clamp$$anonymous$$agnitude simply does this:

 public static Vector2 Clamp$$anonymous$$agnitude(Vector2 vector, float maxLength)
 {
     if (vector.sqr$$anonymous$$agnitude > maxLength * maxLength)
     {
         return vector.normalized * maxLength;
     }
     return vector;
 }

So it only normalizes the vector if it's larger than maxLength.

avatar image
0

Answer by Rocketman_Dan · Aug 18, 2016 at 01:11 PM

@DiegoSLTS Yeah that was a typo. I did use the parentheses to in .Normalize(); but that wasn't a direct copy paste.

I suspected that something else in my script was overriding it somehow and was cleaning up my whitespace and comments to post it here and it started to make it move. There must have been something I forgot to comment out amoungst the other comments.

While it wasn't working it returned (0.7, 0.7) as the full positive Y, full positive X vector and maxSpeed was 5 as intended. These still are the same.

Now however it now treats the mouse point as the positive direction in the world (making controls incredibly clunky) where my intention was to have WASD control the position on the screen within the main camera and the look rotation be independent. How might I overcome that. Perhaps make the player a child of an empty object with the movement part of the scrip on it?

Here is the full script:

 using UnityEngine;
 using System.Collections;
 
 
 public class PlayerMovement : MonoBehaviour
 {
 
     public float maxSpeed = 5f;
     public float shipBoundaryRadius = 0.4f;
     public int rotationOffset = -90;
     float logDelay = 0;
     Vector2 moveDirection;
 
     Rigidbody2D playerRigidbody;
 
     // Update is called once per frame
     void FixedUpdate()
     { 
         //translation controls
 
         /*
         pos.y += Input.GetAxis("Vertical") * Time.deltaTime * maxSpeed;
         pos.x += Input.GetAxis("Horizontal") * Time.deltaTime * maxSpeed;
         */
 
         moveDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
         moveDirection = moveDirection.normalized;
         transform.Translate(moveDirection * Time.deltaTime * maxSpeed);
         Debug.Log("moveDirection is " + moveDirection.normalized);
         Debug.Log("max speed is " + maxSpeed);
 
 
         //decrement delay time for outputting boudary strike info to log
         logDelay -= Time.deltaTime;
 
         Vector3 pos = transform.position;
 
         //restrict ship to boundary of screen
         if (pos.y + shipBoundaryRadius > Camera.main.orthographicSize)
         {
             pos.y = Camera.main.orthographicSize - shipBoundaryRadius;
             if (logDelay <= 0)
             {
                 Debug.Log("PlayerShip boundary strike - restrict to screen");
                 logDelay = 0.5f; // to prevent log from being filled with logs of this collision
             }
 
         }
         if (pos.y - shipBoundaryRadius < -Camera.main.orthographicSize)
         {
             pos.y = -Camera.main.orthographicSize + shipBoundaryRadius;
             if (logDelay <= 0)
             {
                 Debug.Log("PlayerShip boundary strike - restrict to screen");
                 logDelay = 0.5f;
             }
         }
 
         float screenRatio = (float)Screen.width / (float)Screen.height;
         float widthOrtho = Camera.main.orthographicSize * screenRatio;
 
         if (pos.x + shipBoundaryRadius > widthOrtho)
         {
             pos.x = widthOrtho - shipBoundaryRadius;
             if (logDelay <= 0)
             {
                 Debug.Log("PlayerShip boundary strike - restrict to screen");
                 logDelay = 0.5f;
             }
         }
         if (pos.x - shipBoundaryRadius < -widthOrtho)
         {
             pos.x = -widthOrtho + shipBoundaryRadius;
             if (logDelay <= 0)
             {
                 Debug.Log("PlayerShip boundary strike - restrict to screen");
                 logDelay = 0.5f;
             }
         }
         //Update the transfprm
         transform.position = pos;
 
         //Rotation controls
         //get vector equal to difference between ship and mouse
         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
         difference.Normalize(); //normalise vector (i.e. make length = 1)
         float rot = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; //find the angle in degrees
         // set ship rotation to that angle with an offset value to alow the code snippet to be use with any object (by default for our purposes is -90)
         transform.rotation = Quaternion.Euler(0f, 0f, rot + rotationOffset);
         //NOTE: lesson learned use this method in future for better accuracy 
     }
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Speed Change Doesn't Apply,Why does changing the speed makes the controls wrong 0 Answers

Move Object With Angle In 2D 1 Answer

How to make LineRenderer in GameObject Facing? 1 Answer

Moving A 2D Object Forward 1 Answer

Using Lerp with 2d only moving partially 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