Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 awiesbrock · Sep 03, 2016 at 03:15 AM · player movementtouch controls

Trouble having character face direction moving

I've seen a few posts about this but I'm new to scripting and having trouble figuring out how to implement some of the different solutions. I have a basic tap-to-touch function that I grabbed from here: http://www.thegamecontriver.com/2014/10/move-to-mouse-click-touch-position-unity.html

But when the character moves, it doesn't change direction or orientation, regardless of where I tap. I'm trying to figure out if I need transform.LookAt or Quaternion.LookRotation, and how I would even implement it - or maybe it's neither but I can't figure it out regardless.

**I should clarify - it's a 3d game with an isometric-style follow camera.

Here's my current code - again, it's just straight from the above link, and works great outside of the rotation issue. Thanks in advance.

 public class TouchMovement : MonoBehaviour {
     private bool flag = false;
     private Vector3 endPoint;
     public float duration = 50.0f;
     private float yAxis;
 
     void Start(){
         yAxis = gameObject.transform.position.y;
     }
 
     void Update () {
 
         if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
         {
             RaycastHit hit;
             Ray ray;
             #if UNITY_EDITOR
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
             #endif
 
             if(Physics.Raycast(ray,out hit))
             {
                 flag = true;
                 endPoint = hit.point;
                 endPoint.y = yAxis;
                 Debug.Log(endPoint);
             }
 
         }
 
         if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)){ //&& !(V3Equal(transform.position, endPoint))){
 
             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1/(duration*(Vector3.Distance(gameObject.transform.position, endPoint))));
         }
         else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) {
             flag = false;
         }
 
     }
 }
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

2 Replies

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

Answer by b1gry4n · Sep 03, 2016 at 03:50 AM

If you are tapping to move the character, that means you have a Vector3 as the "destination" or in your case "endpoint". This will be the critical part of making the object turn towards that point.

If you are using lookat, the object you are applying lookat to will have its Z axis face the point youve plugged in. that means any rotation value as it will look directly at it. If you only want its Y axis to rotate do something like this:

 player.transform.LookAt(endpoint);
 player.transform.localEulerAngles = new Vector3(0,player.transform.localEulerAngles.y,0);

For Quaternion.LookRotation youll want to plug in the direction to the destination. You get that by doing something like this.

 Vector3 dir = (endpoint- player.transform.position).normalized;
 player.transform.rotation = Quaternion.LookRotation(dir);
 //Again you will have to zero out any rotation that you do not want to happen
 player.transform.localEulerAngles = new Vector3(0,player.transform.localEulerAngles.y,0);
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 awiesbrock · Sep 04, 2016 at 02:28 AM 0
Share

Thanks! Both options rotated accurately. It's super choppy a and no longer translates all the way to touched position, but hey, one problem at a time.

Thanks again!

avatar image b1gry4n awiesbrock · Sep 04, 2016 at 03:28 AM 1
Share

Youll want to lerp the rotation using either Vector3.Lerp or Quaternion.Lerp

avatar image
0

Answer by SoraMahiro · Sep 03, 2016 at 05:22 AM

I don't know much about android development, but I would try to set Transform.LookAt to equal the position in which you touch. If that doesn't work, you could always try setting up the transform.rotation to equal a particular Quaternion.Slerp when you touch.

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make touch buttons stop working after finger slides off. 1 Answer

Event Trigger Pointer Exit sometimes not recognized 0 Answers

Touch controls for Pong based video games for Android 0 Answers

How to make basic touch screen controls 1 Answer

Touch controls for Pong game for Android devices 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