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 hollym16 · Apr 14, 2014 at 10:48 AM · rotationgameobjectraycastvector3hit.point

GameObject facing hit.point doesn't always work

I've got a script that makes a character turn to face the hit.point then move towards it while playing a walk animation. This all works great, most the time. Sometimes, when it is commanded to walk somewhere, it will walk backwards or diagonally. This usually happens if you press different areas quickly or if you press close behind the character when its stationary. The piece of relevant code is as follows:

 private var pointToGo : Vector3 = Vector3.zero

 function Update () 
 {
        if(Input.GetMouseButtonDown(0))
        {
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if(Physics.Raycast(ray, hit)){
             
                 if(hit.transform.name == "NavBar")
                 {
            Zombie.transform.rotation = Quaternion.LookRotation(transform.position - hit.point);


I've put the function before the move to hit.point and the animations so I'm not sure why it is doing this. Anyone know the answer?

Comment
Add comment · Show 2
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 robertbu · Apr 14, 2014 at 02:15 PM 0
Share

Any chance the pivot point is not in the center of the character? Can we see the movement code as well?

avatar image hollym16 · Apr 14, 2014 at 02:46 PM 0
Share

The code that comes after what I've already posted is as follows:

  pointToGo = hit.point;
                 }
            }
         }
         Zombie.transform.position = Vector3.$$anonymous$$oveTowards(Zombie.transform.position, pointToGo, Time.deltaTime * speed);
         if(Zombie.transform.position==pointToGo)
         is$$anonymous$$oving=false;
         if(is$$anonymous$$oving){
         ZombieAnim.animation.CrossFade("Walk Legacy");
         }
         else{
         ZombieIdle.animation.CrossFade("Idle Legacy");
         }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FinKone · Apr 14, 2014 at 11:49 AM

Check to make sure that you are hitting NavBar (debug the name of what youve hit) if its just for moving perhaps ignore all layers with the raycast except NavBar. You could also make this block of code its own method incase you need to use it in something else. Tried to leave this as a comment, phones not seeing the option. Let me kmow if this is the issue if not I'll try to help out more.

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 hollym16 · Apr 14, 2014 at 11:56 AM 0
Share

I've only got the character and the NavBar (which is an invisible Plane) in the scene so it can't be hitting anything else. It's looks like it doesn't have enough time to turn around so just goes in the direction its already facing in. Is there a way I could say "When hit.point is detected, rotate to face it first, then once that is finished, move towards hit.point"?

avatar image robertbu · Apr 14, 2014 at 02:13 PM 0
Share

@hollym16 - ti$$anonymous$$g is likely not your issue. Your last line of code causes an immediate rotation.

avatar image hollym16 · Apr 14, 2014 at 02:33 PM 0
Share

Thats what I thought, I don't know whats causing it

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

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

Related Questions

Unity Rotate Raycast on Quaternion 1 Answer

Rotate a gameobject like car tire 2 Answers

C# Raycast from Object direction (z-axis) + another Vector3 1 Answer

Reverse object position order 1 Answer

Moving gameobject along terrain based on player movement using raycast 1 Answer


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