• 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 Azaroth99 · Jul 01, 2014 at 04:29 PM · playermobileplatformer

How do I make my player face the direction its moving

Hi guys, i'm creating a game for android and i'm using unity mobile assets- sidescrollerSetup. How can I make the player face the direction is moving? I mean when joystick is dragged left, he faces left, when joystick is dragged to right, he faces right? yeah, and here is the code i use:

 #pragma strict
 
 @script RequireComponent( CharacterController )
 
 // This script must be attached to a GameObject that has a CharacterController
 var moveTouchPad : Joystick;
 var jumpTouchPad : Joystick;
 
 var forwardSpeed : float = 4;
 var backwardSpeed : float = 4;
 var jumpSpeed : float = 16;
 var inAirMultiplier : float = 0.25;                    // Limiter for ground speed while jumping
 
 private var thisTransform : Transform;
 private var character : CharacterController;
 private var velocity : Vector3;                        // Used for continuing momentum while in air
 private var canJump = true;
 
 function Start()
 {
     // Cache component lookup at startup instead of doing this every frame        
     thisTransform = GetComponent( Transform );
     character = GetComponent( CharacterController );    
 
     // Move the character to the correct start position in the level, if one exists
     var spawn = GameObject.Find( "PlayerSpawn" );
     if ( spawn )
         thisTransform.position = spawn.transform.position;
 }
 
 function OnEndGame()
 {
     // Disable joystick when the game ends    
     moveTouchPad.Disable();    
     jumpTouchPad.Disable();    
 
     // Don't allow any more control changes when the game ends
     this.enabled = false;
 }
 
 function Update()
 {
     var movement = Vector3.zero;
 
     // Apply movement from move joystick
     if ( moveTouchPad.position.x > 0 )
         movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
     else
         movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
     
     // Check for jump
     if ( character.isGrounded )
     {        
         var jump = false;
         var touchPad = jumpTouchPad;
             
         if ( !touchPad.IsFingerDown() )
             canJump = true;
         
          if ( canJump && touchPad.IsFingerDown() )
          {
             jump = true;
             canJump = false;
          }    
         
         if ( jump )
         {
             // Apply the current movement to launch velocity        
             velocity = character.velocity;
             velocity.y = jumpSpeed;    
         }
     }
     else
     {            
         // Apply gravity to our velocity to diminish it over time
         velocity.y += Physics.gravity.y * Time.deltaTime;
                 
         // Adjust additional movement while in-air
         movement.x *= inAirMultiplier;
 //        movement.z *= inAirMultiplier;
     }
         
     movement += velocity;    
     movement += Physics.gravity;
     movement *= Time.deltaTime;
     
     // Actually move the character    
     character.Move( movement );
     
     if ( character.isGrounded ){ {
         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
         moveDirection *= speed;
 
         if (moveDirection.sqrMagnitude > 0.01)
             transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (moveDirection), 90);
     }
 
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);           
 }
         // Remove any persistent velocity after landing    
         velocity = Vector3.zero;
 }

Comment
Add comment · Show 13
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 Azaroth99 · Jul 01, 2014 at 02:01 PM 0
Share

Sorry, i should have notice that

avatar image Azaroth99 · Jul 02, 2014 at 10:27 AM 0
Share

Harshad$$anonymous$$, can you help me with this please?

avatar image HarshadK · Jul 02, 2014 at 10:40 AM 1
Share

I haven't read all your script but see if this forum thread helps: transform.LookAt or Quaternion.LookRotation on 1 axis only?

avatar image Azaroth99 · Jul 02, 2014 at 11:05 AM 0
Share

Thank you man :D

avatar image Azaroth99 · Jul 02, 2014 at 12:30 PM 0
Share

do you know how to make this using euler angles, or transform.lookAt?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NikunjPopat · Jul 02, 2014 at 01:11 PM

 // Apply movement from move joystick
 
 if ( moveTouchPad.position.x > 0 )
         movement = transform.right * forwardSpeed * moveTouchPad.position.x;
 else
         movement =-transform.right * backwardSpeed * moveTouchPad.position.x;


Use transform.right instead of Vector3.right. It will provide you local rotation...

Comment
Add comment · Show 4 · 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 Azaroth99 · Jul 02, 2014 at 01:30 PM 0
Share

Assets/Standard Assets ($$anonymous$$obile)/Scripts/SidescrollControl.js(55,50): BCE0020: An instance of type 'UnityEngine.Transform' is required to access non static member 'right'.

what should i do? This appears when change from vector3.right to transform.right

avatar image Azaroth99 · Jul 02, 2014 at 01:36 PM 0
Share

ok solved, but my character is moving now on Z axis

avatar image Azaroth99 · Jul 05, 2014 at 09:14 AM 0
Share

please? ...

avatar image NikunjPopat · Jul 07, 2014 at 06:06 AM 0
Share

now you should try transform.up and others

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

I'm making a 2D mobile platformer and I just figured out how to make the mobile buttons work. But I can't get the jump limit to work. Does anyone know how to create maybe a ground detection with this script? 1 Answer

Number rain background 2 Answers

Mobile platformer jump not working 2D 1 Answer

Why do I double jump? This isn't supposed to happen... 2 Answers

I am making a Call of Duty type game, how can I make the people settings just like call of duty? 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