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 Jin · Mar 14, 2010 at 11:45 PM · charactercontrol

About Character Control :D

Hello there~! I am wondering if anyone can tell me why my character always will face forward after finishing of control. Following are my scripts and Thanks in advance!

var moveSpeed = 5.0;

var rotateSpeed = 80.0;

private var controller :CharacterController;

controller = gameObject.GetComponent(CharacterController);

private var moveDirection = Vector3.zero;

private var forward = Vector3.forward;

private var right = Vector3.right;

function Update() {

 var x = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed;

 var z = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed;

 var targetDirection = x * right + z * forward;

 transform.Translate(x,0,z,Space.World);

 var angle = Mathf.Atan2(x,z)* Mathf.Rad2Deg ;

 transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);

}

If you have a better way of controlling it like mine and would like to share it with me, then I will very appreciate it~! :)

Comment
Add comment · Show 3
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 Jin · Mar 14, 2010 at 11:50 PM 0
Share

By the way, it faces world's forward~!

avatar image Lipis · Mar 15, 2010 at 12:18 AM 0
Share

If you can also edit the post, select your code.. and press the Code Sample button, to be awesome..

avatar image Jin · Mar 15, 2010 at 03:34 AM 0
Share

Thank you~ I got it~

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Jaap Kreijkamp · Mar 15, 2010 at 12:52 AM

Don't understand your question completely but you DON'T want the character face the direction you're moving? Then remove the transform.rotation line at the end.

If you WANT to have it look in the right direction, you could use transform.LookAt(transform.position + targetDirection, Vector3.up), although you should check if targetDirection.sqrMagnitude > 0. So that would give:

var moveSpeed = 5.0; var rotateSpeed = 80.0; private var controller :CharacterController; controller = gameObject.GetComponent(CharacterController); private var moveDirection = Vector3.zero; private var forward = Vector3.forward; private var right = Vector3.right;

function Update() { var x = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed; var z = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed;

 var targetDirection = x * right + z * forward;

 transform.Translate(x,0,z,Space.World);

 if (targetDirection.sqrMagnitude > 0) {
     transform.LookAt(transform.position + targetDirection, Vector3.up);
 }

}

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 Jin · Mar 15, 2010 at 03:14 AM 0
Share

Thank you very much~! It is much better now , but still it can only look at 8 directions--(1,0,0),(1,0,1),(0,0,1),(1,0,-1),(0,0,-1),(-1,0,-1),(-1,0,0),(-1,0,1).. Could you please tell me if there is a better solution?

avatar image Jaap Kreijkamp · Mar 15, 2010 at 05:51 AM 0
Share

Basically that's a problem of your control setup, if you would use a analogue controller you would see you can actually look in every direction. You can edit under Edit->ProjectSettings->Input the behaviour of the control keys, make the keys react slower (more time to move from centre position to max position).

avatar image Jin · Mar 15, 2010 at 06:21 AM 0
Share

O$$anonymous$$, I see. Thank you for your time! :)

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

No one has followed this question yet.

Related Questions

How to make camera position relative to a specific target. 1 Answer

Character ragdoll to character animation? 2 Answers

Sprite animation not functioning correctly. 0 Answers

Character Rotation point at Mouse 1 Answer

Player slows down when hitting an incline 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