Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by dizyo · Mar 25, 2013 at 01:13 AM · rotationcharacter

How to control a third person characters facing direction

Here's the link to the game I'm working on.

https://dl.dropbox.com/u/54318296/deez_world/WebPlayer/WebPlayer.html

Use arrow keys to move the player, space to jump, and a key to punch. I'm having trouble coding the character to face the direction he is moving in.

This is the code used.

 private var controller : CharacterController; 
 
 
 
 
 
 function Start ()
 
 
 
 {
 
     controller = GetComponent(CharacterController); 
 
     animation.wrapMode = WrapMode.Loop;
 
  
 
     animation["jump"].layer = 10;
 
     animation["jump"].wrapMode = WrapMode.ClampForever;
 
  
 
     animation["fall"].layer = 10;
 
     animation["fall"].wrapMode = WrapMode.ClampForever;
 
  
 
     animation["land"].layer = 10;
 
     animation["land"].wrapMode = WrapMode.Once;
 
     
 
 
 
     
 
     animation.Stop();
 
     animation.Play("idle");
 
 }
 
  
 
 function Update ()
 
 {
 
     var MovementY = controller.velocity.y;
 
     var controller : CharacterController =  GetComponent(CharacterController);
 
  
 
     if ((MovementY > 0.1) && !(controller.collisionFlags & CollisionFlags.Below))
 
     {
 
     animation.CrossFade("jump");
 
     }
 
     else if ((MovementY < -5) && !(controller.collisionFlags & CollisionFlags.Below))
 
     {
 
     animation.CrossFade("fall");
 
     }
 
  
 
     if ( ((animation.IsPlaying("jump")) || (animation.IsPlaying("fall"))) && (controller.collisionFlags & CollisionFlags.Below))
 
     animation.CrossFade("land");
 
  
 
     
 
     else
 
       animation.CrossFade("idle");
 
       
 
     
 
     if (Input.GetKey("down"))
 
         animation.Play("backrun");
 
     
 
      if (Input.GetKey("up"))
 
          animation.Play("run");    
 
         
 
     
 
     if (Input.GetKey("a"))
 
         animation.CrossFade ("punch");
 
         

}

I having trouble making my player rotate properly and making the character stay facing towards direction of the last key pressed.

Any suggestions?

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 robertbu · Mar 25, 2013 at 03:19 AM

My experience with character controllers and animations is very limited, but conceptually this the code might solve your problem. Place it in Update().

 var lookPos = controller.velocity;
 lookPos.y = transform.position.y;
 if (lookPos.sqrMagnitude > 0.0) {
     lookPos += transform.position;
     transform.LookAt(lookPos);
 }
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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Undesirable character flipping 0 Answers

Turn in the direction of movement 0 Answers

[HELP] Rotate Bullet Script 0 Answers

keeping the player upright 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