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 uno · Aug 01, 2012 at 11:16 AM · rotateplatformercharacter controller2.5d

rotate character in 2.5D

Hi all,

I am trying to learn character controller in Unity3D, i have a basic setup of platform (similar to 2d platform tutorial provided with unity), and character, my character move through character controller in right/left direction when i press right/left arrow key, now i want my character to rotate towards the arrow direction (i.e. face towards right when i press right key & face left when i press left arrow key).

i want my character to move in only left/right direction, and not in z axis. i am unable to figure out how to do that with c# script.

my code attached with character is as follow -

public class PlayerController : MonoBehaviour { public float speed = 6.0F; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Vector3 moveDirection = Vector3.zero; CharacterController controller; void OnControllerColliderHit (ControllerColliderHit hit) { if(hit.collider.name == "pickup_coin"){ Destroy(hit.collider.gameObject); } } // Update is called once per frame void Update () { controller = GetComponent(); 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; manageAnimation(); controller.Move(moveDirection * Time.deltaTime); transform.position = new Vector3(transform.position.x, transform.position.y, 0); // make sure it will always remain in 0 @ z-depth. } /// /// Manages the animation's state according to speed. /// private void manageAnimation() { if(!controller.isGrounded){ animation.CrossFade("jump"); }else{ if(moveDirection.x != 0){ Debug.Log(transform.rotation.eulerAngles.y); animation.CrossFade("walk"); if(moveDirection.x > 0 && transform.rotation.eulerAngles.y != 90){ transform.Rotate(0, 90, 0); }else if(moveDirection.x < 0 && transform.rotation.eulerAngles.y != 270.0f){ transform.Rotate(0, 270, 0); } }else{ animation.CrossFade("idle"); } } } }

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 Radon · Aug 03, 2012 at 03:06 AM

Try rotation at every unit.

 function Update() {
 if(Input.GetAxis ("Horizontal") || Input.GetAxis ("Vertical"){
     transform.Translate(x, y, z); // change x, y, z with #s
     }
 }
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 uno · Aug 03, 2012 at 10:17 AM 0
Share

Hi @Radon, thanks for input, above code will move character in all the direction, but i wanted to restrict it in x direction only.

anyway, i really appreciate your effort for jumping into such a lengthy question.

now i got my answer by myself, ins$$anonymous$$d of rotation character, i make that character a child of empty game object, & applied script onto it, now i am only rotation child character, that thing worked for me. thanks once again for your kind suggestion.

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

7 People are following this question.

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

Related Questions

Grappling Hook in 2.5d Game 0 Answers

Camera rotation around player while following. 6 Answers

Roll a Ball using Character Controller 0 Answers

Why is my character not flipping? 2 Answers

How to keep character facing 3/4 front when moving right and left in 2.5D game 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