• 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 superventure · Feb 10, 2011 at 02:24 AM · movementplayerdirectionfacekeys

Have a character face their moving direction?

I am workng with this script

var controller : CharacterController = GetComponent(CharacterController);

var forward = transform.TransformDirection(Vector3.forward);

var curSpeed = speed * Input.GetAxis ("Vertical");

transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 8);

controller.SimpleMove(forward * curSpeed);

It works by rotating the player with the left and right arrows, and actually moving the character forward/backwards with the veritcal keys. How do I manipulate THIS script to make the character walk AND face a direction with the left and right keys just like the forward and backward keys?

I've tried making a 2nd curSpeed var:

var curSpeed2 = speed*(Input.GetAxis("Horizontal");

but I am lost when it comes to plugging that in to the rest of the code...

controller.SimpleMove(forward * curSpeed + curSpeed2);

doesn't work.

Any help is appreciated!

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
0

Answer by Peter G · Feb 10, 2011 at 02:29 AM

Here is the easiest way:

if(forward.magnitude > .2) {
//Add a conditional to prevent snapping back
    transform.forward = forward;
    //Set the transform's forward Vector to your move direction.
}

There are move complicated things you can do with rotation if you want different effects such as smooth rotation, but this will have your character snap to the direction they are moving.

//Off the top of my head //There are some snapping back issues, you would need to rearrange variable's scope and conditionals. //So, sorry, this code is only partially functional. It's more about the concept.

if(forward.magnitude > .2) { //Add a conditional to prevent snapping back var desiredRotation = Quaternion.LookRotation(forward); transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, Time.deltaTime); }

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 superventure · Feb 10, 2011 at 02:47 AM 0
Share

I understand the logic here ( move in any direction-forward/back/left/right and face that way), but when I applied it to the script, the player stopped rotating completely. What am I missing here?

avatar image Peter G · Feb 10, 2011 at 02:55 AM 0
Share

Well, when I look at your code, you cannot add a scalar type (a float) and a Vector type as you do in your last snippet so you need some parenthesis for order of operations.

avatar image Peter G · Feb 10, 2011 at 02:56 AM 0
Share

As far as movement goes, you probably want to make a new Vector not based on the character's direction. moveDirection = new Vector3(HorizontalInput moveSpeed, 0, VerticalInput moveSpeed);

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

Push player in direction based on objects movement 1 Answer

move particle direction with player 1 Answer

How to detect two keys released simultaneously [Input System] 1 Answer

Face forward direction of movement 3 Answers

Make the character face the direction he is moving? 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