• 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 Disaster · Jun 20, 2010 at 05:33 PM · gameobject

Problem with moving gameObject, especially diagonally!

I am trying to simply character controller without using a rigidbody (since I don't need any physics.) The results are ok, but for some reason my gameObject moves faster when moving diagonally than when just heading in a single direction. Here is my code:

void FixedUpdate() { float xPos = Input.GetAxis("Horizontal") * _player.speed * Time.deltaTime; float yPos = Input.GetAxis("Vertical") * _player.speed * Time.deltaTime;

     Vector3 newPos = new Vector3(xPos, 0, yPos);
     newPos = Camera.main.transform.TransformDirection(newPos);
     _position = _position + newPos;
     _position.y = _player.transform.position.y;

     _direction = new Vector3(xPos, 0, yPos);
     _direction = Camera.main.transform.TransformDirection(_direction);
     _direction.y = 0;
 }

 void Update() { 
     if(_direction.sqrMagnitude > 0.01)
         _player.transform.rotation = Quaternion.Slerp (_player.transform.rotation, Quaternion.LookRotation (_direction), Time.deltaTime * 10);

     _player.transform.position = Vector3.Slerp(_player.transform.position, _position,Time.deltaTime*10);
 }

If anyone has any suggestions as to how I can get around this problem or perhaps even improve the above code, I'd really appreciate it!

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

Answer by Tetrad · Jun 20, 2010 at 05:51 PM

Change the first three lines to this:

float xPos = Input.GetAxis("Horizontal"); float yPos = Input.GetAxis("Vertical");

 Vector3 newPos = new Vector3(xPos, 0, yPos);
 if( newPos.magnitude > 1.0f ) 
 {
      newPos = newPos.normalized;
 }
 newPos *= _player.speed;
 newPos *= Time.deltaTime;

Since Input goes from -1 -> 1, you'll want to handle the case where x and y are both 1. The length of that is sqrt(2) if you do the trig. You want to just clamp it to 1.

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 Disaster · Jun 20, 2010 at 05:59 PM 0
Share

Thanks a lot, this works great. I also found this code on the forums which has much the same effect... I don't think one is any faster than the other but others may find this useful:

float xPos = Input.GetAxis("Horizontal") _player.speed Time.deltaTime;

float yPos = Input.GetAxis("Vertical") _player.speed Time.deltaTime;

float input$$anonymous$$odifyFactor = (xPos != 0.0f && yPos != 0.0f) ? .7071f : 1.0f;

Vector3 newPos = new Vector3(xPos*input$$anonymous$$odifyFactor, 0, yPos*input$$anonymous$$odifyFactor);

avatar image Tetrad · Jun 20, 2010 at 06:06 PM 0
Share

That will still have the same problem, except ins$$anonymous$$d you'll max out at speed ins$$anonymous$$d of sqrt(2) * speed.

avatar image
-1

Answer by AnaRhisT · Jun 20, 2010 at 05:38 PM

Diagonally u mean when u press 2 keys like w + d in the same time? if yes, u can make a check and if it matches then to decrease the speed by 2.

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.

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

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

Why does IsSleeping() keep returning false? (Billiard Logic) 0 Answers

Positioning Textures on screen 0 Answers

Making a game object un-render or turn-invisible through trigger? 1 Answer

Queuing Gameobjects 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