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 Musty · Sep 18, 2013 at 02:09 PM · movementraycastfacing

Click to move and facing to move direction?

Hey guys, So im trying to make a top down game where you click to move, everything is working besides the player doesn't face the direction its moving properly. Sometimes it just moves backwards towards the place clicked, i was wondering if anyone can see what ive done wrong here, any help is appreciated.

 using UnityEngine;
 using System.Collections;
 
 public class movement : MonoBehaviour
 {
     public Ray ray;
     public GameObject player;
     public Vector3 newPos;
     RaycastHit hit;
     Vector3 moveDir;
     
     
     
     // Use this for initialization
     void Start ()
     {
     
     }
     
     // Update is called once per frame
     void Update ()
     {
         ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
         if (Physics.Raycast (ray, out hit)) 
         {
 
             if (Input.GetMouseButton (0)) 
             {
                 newPos = new Vector3 (hit.point.x, 2.2f, hit.point.z);
                 player.transform.position = Vector3.Lerp (player.transform.position, newPos, Time.deltaTime);
                 moveDir = newPos;
                 moveDir = new Vector3 (newPos.x, 0.0f, newPos.z);
                 player.transform.rotation = Quaternion.LookRotation(moveDir * rotationSpeed);
                     
             }
         }
     }
 }
 

 
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
Best Answer

Answer by robertbu · Sep 18, 2013 at 02:20 PM

Your problem is that LookRotation() takes a vector relative to the characters position, not a world point. I'm assuming you are using the '2.2f' to move the hit point up to the level of the character's pivot. If so, you can fix your code in a couple of different ways. First you could eliminate lines 31 - 33 and replace them with:

 player.transform.LookAt(newPos);

Or if you still want to use LookRotation:

 player.transform.rotation = Quaternion.LookRotation(newPos - player.transform.position);
Comment
Add comment · Show 3 · 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 Musty · Sep 18, 2013 at 03:12 PM 0
Share

hey thanks for the reply, i tried what you suggested it still has the problem, if you hold the mouse button it works fine, but if you click on different spots it doesnt rotate and move to that, it simply moves to that spot,

avatar image robertbu · Sep 18, 2013 at 04:38 PM 0
Share

In your original questions you say, "everything is working besides the player doesn't face the direction its moving properly," so I'm guessing on how you want this to function. Your rotation and movement code is inside the '`if (Input.Get$$anonymous$$ouseButton(0)`', so it is only going to move/rotate if the mouse button is pressed down. As a guess, you might be looking for this functionality:

 using UnityEngine;
 using System.Collections;
  
 public class $$anonymous$$ovement3 : $$anonymous$$onoBehaviour
 {
     public GameObject player;
     public float speed = 1.0f;
     private Vector3 newPos;
  
     void Update ()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0)) 
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             if (Physics.Raycast (ray, out hit)) 
             {
                 newPos = new Vector3 (hit.point.x, 2.2f, hit.point.z);
             }
          }
         
         player.transform.position = Vector3.Lerp (player.transform.position, newPos, Time.deltaTime * speed);
         player.transform.LookAt(newPos);
     }
 }
avatar image Musty · Sep 18, 2013 at 05:24 PM 0
Share

Thanks for your help!! i thought it was more complicated than that.

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

15 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

Related Questions

Paddle script for Pong-based video game for Android 0 Answers

Stop the movement to a direction 0 Answers

Dashing through enemies, but not walls. 1 Answer

Can I animate objects/cameras in Update() without breaking Physics.Raycast? 1 Answer

Moving on the uneven terrain with raycasting 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