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
0
Question by weiloon1234 · Apr 01, 2014 at 10:20 AM · 2d2d-platformermove an objectcharacter controlling

joystick changing my character facing direction and moving within screen only

i'm new to unity3d, i readed a guide here

and i using a joystick controller that can easily found it using google name MPJoystick

i try to change the mouse click move to move using joystick, now my character can move, but the character facing direction won't, i have no idea why.

1 april 2014 9:50PM: after some try, finally i can change the facing direction, next i need to control the character inside the screen and not go over, anyone know how?i tried using the Matf.Clamp solutions that i found but not working, no idea why

here is my code

controller.cs

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour {
     
     public float moveSpeed;
     public float screenWidth;
     public float screenHeight;
     
     public MPJoystick moveJoystick;  
 
     private Vector3 moveDirection;
     public Vector3 faceDirection;
 
     public float turnSpeed;
 
     public Vector3 playerSize;
     public float distance;
     public float leftBorder;
     public float rightBorder;
     public float bottomBorder;
     public float topBorder;
 
     public void Awake() {
         
     }
 
     void Start () {
         screenWidth = Screen.width;
         screenHeight = Screen.height;
         playerSize = renderer.bounds.size;
         distance = (transform.position - Camera.main.transform.position).z;
         leftBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).x + (playerSize.x/2);
         rightBorder = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance)).x - (playerSize.x/2);
         
         bottomBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).y + (playerSize.y/2);
         topBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, distance)).y - (playerSize.y/2);
 
         faceDirection = Vector3.right;
     }
 
     void Update () {
         Vector3 currentPosition = transform.position;
         float touchKey_x =  moveJoystick.position.x;
         float touchKey_y =  moveJoystick.position.y;
 
         if (touchKey_x == 0 && touchKey_y == 0) {
             return;
         } else {
 
             /* Change facing direction */
             faceDirection.y = moveDirection.y + touchKey_y;
             faceDirection.x = moveDirection.x + touchKey_x;
             float targetAngle = Mathf.Atan2(faceDirection.y, faceDirection.x) * Mathf.Rad2Deg;
             transform.rotation = 
                 Quaternion.Slerp( transform.rotation, 
                                  Quaternion.Euler( 0, 0, targetAngle ), 
                                  turnSpeed * Time.deltaTime );
 
             /* i think the next 3 line is change the facing direction? am i right? answer: i think i'm wrong */
             moveDirection.x = moveDirection.x + touchKey_x;
             moveDirection.y = moveDirection.y + touchKey_y;
             moveDirection.Normalize ();
 
             /* i guess here is the moving code? am i right? */
             Vector3 target = moveDirection * moveSpeed + currentPosition;
             /*target.x = Mathf.Clamp (target.x, leftBorder, rightBorder);
             target.y = Mathf.Clamp (target.y, topBorder, rightBorder);*/
             transform.position = Vector3.Lerp (currentPosition, target, Time.deltaTime);
         }
     }
 }


and how can i avoid the character go over the screen? if i try without the target.x = Math.Clamp blablabla and target.y = Math.Clamp blablabla, the character can move well but the problem is the character can go over the screen,

but when i try using Math.Clamp, the character move become so weird, sometime i move top but go bottom, i move bottom but go top, sometime stuck at center but sometime can go over...

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

0 Replies

· Add your reply
  • Sort: 

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d Movement Jumps to the Left 3 Answers

Using animated transform to push object away? 2D 0 Answers

2d gameplay movement script 1 Answer

2D Detect collisions of a 2D block only on left/right (not top/bottom) 0 Answers

Type"PathDefinition" does not contain a definition for "GetPathEnumerator" 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