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 /
avatar image
1
Question by JUnityer · Dec 21, 2011 at 01:11 PM · platformerlookat2.5dturningmovedirection

Only look at the moveDirection of Z-axis

I'm making a 2.5D sidescroller game, and I need my character to face the direction he's moving. My scene is setup so that Characters move forward and backward along the z axis, and up and down along the y axis.

So, In my current script I have managed to make my Character turn back and forth while he's grounded. But when he jumps, he also turns to face up and down, because of the following piece of code (at least I think so):

 if (Input.GetAxis("Horizontal"))
 {
     transform.LookAt(transform.position + moveDirection);
 }

So, what do I need to add here to make the character only look at the moveDirection of z-axis?

Ps. I have a strong feeling that the problem might be that line, but I can post the whole script if you think the problem is elsewhere.

EDIT

So, here's a little pic that shows how my scene is positioned, and how my character turned in the original script, while he was grounded.

http://imageshack.us/photo/my-images/40/kuva1tb.jpg/

The red capsule is the character, and the green thing is his gun, which points at the direction he's facing.

And this is what happens if I jump:

http://imageshack.us/photo/my-images/683/kuva2x.jpg/

I'm trying to make it turn in air just the same way as if it were grounded.

And just as a bonus, here's my whole current script with your editings:

 var speed : float = 20.0;
 var jumpSpeed : float = 20.0;
 var gravity : float = 30.0;
 private var moveDirection : Vector3 = Vector3.zero;

 function Update() 
 {
     var controller : CharacterController = GetComponent(CharacterController);
     var inputZ = Input.GetAxis("Horizontal");
     if(Input.GetAxis("Horizontal"))
     {
         var tempDirection = moveDirection;
         tempDirection.z = transform.position.z;
         transform.LookAt(transform.position + tempDirection);
     }
     if (controller.isGrounded) 
     {
         moveDirection = Vector3(0, 0, (Input.GetAxis("Horizontal")));
         moveDirection *= speed;
         if (Input.GetButton ("Jump")) 
         {
             moveDirection.y = jumpSpeed; 
         }
     }
     else
     {
         moveDirection.z = inputZ * speed;
     }
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }
Comment
Add comment · Show 5
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 Aleron · Dec 21, 2011 at 06:21 PM 0
Share

And what is it you want the character to actually do when he jumps?

avatar image JUnityer · Dec 21, 2011 at 06:30 PM 0
Share

I only want him to go upwards, where I can still move him left and right and turn him to face left or right, and then fall down, while still being in control of him. He shouldn't turn sideways when he jumps, but stand "on his feet", just like when he's grounded. :)

Your code makes him act very strangely, he rolls around when he passes the 0 world point. Sorry if I'm being unclear, I don't know all important words, since English is not my number 1 language!

avatar image Aleron · Dec 21, 2011 at 06:48 PM 0
Share

Ahh, I see. Yeah, the LookAt call will basically be trying to make him look at his feet with the way it is set up.

What's the behavior if you take out the entire if( Input.GetAxis("Horizontal") ) chunk of code? Does the player just jump up and down without being able to turn him to face left or right?

avatar image JUnityer · Dec 21, 2011 at 07:00 PM 0
Share

Yes, then he can move left and right, but he just faces right always, never being able to turn left. But Bunny's solution seems to be working just perfectly. I'm going to give you an upvote anyway, for your help and patience :)

avatar image Aleron · Dec 21, 2011 at 07:07 PM 0
Share

Thanks JUnityer, sorry I led you down the wrong path for so long! As soon as I saw it I knew Bunny's solution was right. :)

2 Replies

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

Answer by Bunny83 · Dec 21, 2011 at 06:48 PM

Just set the y-component of your moveDirection to 0 before you use it for LookAt, or since it's a 2.5d game, just use the z axis

 // Just use the z axis
 transform.LookAt(transform.position + Vector3(0,0,moveDirection.z));

 // Or set the y axis to 0
 var tempDirection = moveDirection;
 tempDirection.y = 0;
 transform.LookAt(transform.position + moveDirection);
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 JUnityer · Dec 21, 2011 at 07:04 PM 0
Share

Thank you! Seems that this was actually too simple for me to realize. I think I have to start studying these transform and Vector3 things a bit more. This saved my project, I'll learn from this!

avatar image
1

Answer by Aleron · Dec 21, 2011 at 04:48 PM

EDIT: This answer is incorrect and should not be used for reference in this case! The selected answer is correct (not to mention simpler!).

If I understand correctly, you only want the look at rotation in the x-y plane, ignoring the player's vertical location (z-axis). If that's the case, a simplistic way to use only the x and y components of the movement can be done with something like the following:

 if(Input.GetAxis("Horizontal"))
 {
     var tempDirection = moveDirection;
     tempDirection.z = transform.position.z;
     transform.LookAt(transform.position + tempDirection);
 }
Comment
Add comment · Show 8 · 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 JUnityer · Dec 21, 2011 at 05:06 PM 0
Share

I'm receiving a semicolon error. It points into this line

 Vector3 tempDirection = moveDirection;
avatar image Aleron · Dec 21, 2011 at 05:11 PM 0
Share

Can you post the exact error?

And I just noticed another error is my response, I used two arguments in LookAt, when I should be adding the two vectors. I'll edit my response to fix that. Then when I see the error you are getting for the earlier line, hopefully I'll see what's going wrong there. :)

avatar image JUnityer · Dec 21, 2011 at 05:38 PM 0
Share

The error was

...blahblah.js(30,16): UCE0001: ';' expected. Insert a semicolon at the end

It points to the line I mentioned before. There are no other errors, only this one.

And the new version doesn't change it anywhere, same error. This really confuses me, it looks like there's nothing wrong with it.

avatar image Aleron · Dec 21, 2011 at 05:43 PM 0
Share

Ahh, javascript. Try my edited version of the code. I also fixed the second line inside the if so that it was using the z component of the transform.position (like it should have been, oops!)

avatar image Bunny83 · Dec 21, 2011 at 06:44 PM 1
Share

Sorry, but this doesn't make much sense ;) tempDirection and moveDirection are directions. That means they are relative to something, in this case the player. Setting one axis of a direction to an absolute position will screw up the direction. Also the z-axis is the forward axis which is the one he want. He don't want any rotation up and down which is the y-axis ;).

Show more comments

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

6 People are following this question.

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

Related Questions

2.5D Melee 0 Answers

Grappling Hook in 2.5d Game 0 Answers

2D or 3D settings for a 2.5D game? 2 Answers

2D 360 degress platformer example needed 0 Answers

2.5D Sprite always face at camera 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