• 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 Vylse · Feb 16, 2012 at 03:40 PM · direction

Look towards Running Direction

so far i have this, but i'm really messed up with making my character face at the direction he's moving

 var speed: float=6.0;

var jumpSpeed: float=8.0; var gravity : float=20.0; private var tr : int =90; private var moveDirection : Vector3 = Vector3.zero;

function Update () { var controller : CharacterController = GetComponent(CharacterController); if(controller.isGrounded) { moveDirection = new Vector3(0,0,Input.GetAxis("Horizontal")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;

     if(Input.GetButton("Jump"))
     {
     moveDirection.y = jumpSpeed;
     }
 }
 
 moveDirection.y -= gravity * Time.deltaTime;
 
 controller.Move(moveDirection * Time.deltaTime);

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ZweiD · Feb 16, 2012 at 07:17 PM

here you have it: Transform.LookAt

Your code could look like this:

 gameObject.transform.LookAt(gameObject.transform.position + moveDirection);

That is the easiest way I found to achieve what you want. Just be carefull that the rotation can be 'jumpy' at times (eg when you stop).

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 Vylse · Feb 17, 2012 at 12:50 AM 0
Share

Thanks!! he can now face the direction but now, i'm having trouble moving him on the direction he's facing. it's like press "d" to move and then "a" to turn.

i'm making a 2d platformer game, Sorry about the lack of information on my post.

avatar image ZweiD · Feb 17, 2012 at 08:58 AM 0
Share

ok, then simply leave out the transform.TransformDirection. that way it should always go in the absolute direction you are giving it.

transformDirection tries to transform the vector you give it from local to global space. but since you want your input to give a global direction (one that isn't dependent on the current character orientation) thats the wrong thing to do.

did that clear things up?

avatar image
0

Answer by Owen-Reynolds · Feb 17, 2012 at 04:01 AM

For 2D, you're always looking left or right? In that case:

 if(MoveDirection.x<0)
   transform.rotation = Quaternion.Euler(0,-90,0);
 else
   transform.rotation = Quaternion.Euler(0,90,0);

There's no way to say "facing right," since it depends on the exact model. This assumes the model is facing "forwards" -- the most common way -- which would have her back to you. (0,90,0) is spun 90 degrees right. You may have to play with the numbers.

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 ZweiD · Feb 17, 2012 at 09:01 AM 0
Share

you are right, your solution is simpler then $$anonymous$$e but this does not help him with the problem he is facing with my solution.

his direction will still depend on the local rotation of his character and therefore end up completely weird for a 2d platformer.

avatar image
0

Answer by Vylse · Feb 18, 2012 at 03:42 AM

thanks guys so far ive come up with both your ideas and made this, but i'm having trouble animating it.

 function FixedUpdate () 

{ if(grounded){ moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,0); moveDirection*=speed;

         if(Input.GetButton("Jump"))
         {
         moveDirection.y=jumpSpeed;
     
         }
     }
 
 else
 {    
     moveDirection = new Vector3(Input.GetAxis("Horizontal"),moveDirection.y/speed,0);
     moveDirection*=speed;
 }
 
 
 moveDirection.y-=gravity*Time.deltaTime;
 
 controller = GetComponent(CharacterController);
 flags = controller.Move(moveDirection*Time.deltaTime*speed);
 
 grounded = (flags & CollisionFlags.CollidedBelow) !=0;
 
 
 
 if(moveDirection.x>0)
 {
 tr = 90;
 }else
 if(moveDirection.x<0){
 tr=270;
 }
 transform.eulerAngles.y-=(transform.eulerAngles.y-tr)/5;
 
 if(Input.GetAxis("Horizontal"))
 {
 animation.CrossFade("MRUN",0.3);
 }
 else
 {
 animation.CrossFade("MIDLE",0.3);
 }
 

}

"MWALK" layer is set to -1; "MIDLE" also

"MJUMP" layer is set to 10 which im having trouble animating him to jump;

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

7 People are following this question.

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

Related Questions

my rocket fires in the wrong direction 4 Answers

player view Rotation check 0 Answers

The direction of travel is not rotating with the projectile in the spinning GameObject. 5 Answers

NPC Transform.Direction relative to player?? 0 Answers

Child object using Origin (0, 0, 0) as its forward direction 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