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 · Jan 28, 2014 at 12:56 PM · animationgameobjectraycastcharacter

Get GameObject to face and move towards Raycast hit point

Hi Guys, I've got the following script on a Plane (the floor) that makes my character snap to the hit point:

 #pragma strict
 var pokeForce : float;
 var transform : Transform;
 var Character : GameObject;
 private var ray: Ray;
 private var hit : RaycastHit;
 
 function Start () {
 }
 function Update () {
 
 if(Input.GetMouseButtonDown(0)){
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit)){
         if(hit.transform.name == "Floor"){
             Character.transform.position = hit.point;
 }
 }
 }
 }

However, I want to improve it in two ways: 1. I want the Character to move smoothly between its current position and the hit point, not snap 2. I want the Character to face the hit point/ the direction its going in

Could anyone point me in the right direction please?

Comment
Add comment · Show 4
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 Sundar · Jan 28, 2014 at 01:25 PM 0
Share

Here are some suggestions

  1. Use Vector3.Lerp to move your character smoothly within a given time from point a to point b

  2. Use Character.transform.LookAt( hit.point ); for looking at a point.

  3. Educate yourself by googling above suggestions

avatar image hollym16 · Jan 29, 2014 at 10:42 AM 0
Share

I've fixed the LookAt(hit.point), thanks for the suggestion Sundar. I've had a fiddle with the Vector3.Lerp and that still seems to either nudge the GameObject in the direction of the hit point, or make it jump to the hit point. Any suggestions on how I could fix it? Here's the script I tried out:

 #pragma strict
 
 var transform : Transform;
 var Zombie : GameObject;
 private var ray: Ray;
 private var hit : RaycastHit;
 // Transforms to act as start and end markers for the journey.
     var start$$anonymous$$arker: Transform;
     var end$$anonymous$$arker: Transform;
     
     // $$anonymous$$ovement speed in units/sec.
     var speed = 1.0;
     
     // Time when the movement started.
     private var startTime: float;
     
     // Total distance between the markers.
     private var journeyLength: float;
     
     var target : Transform;
     var smooth = 5.0;
 
 function Start () {
 
     // $$anonymous$$eep a note of the time the movement started.
         startTime = Time.time;
         
         // Calculate the journey length.
         journeyLength = Vector3.Distance(start$$anonymous$$arker.position, end$$anonymous$$arker.position);
     }
     
 
 
 function Update () {
         // Distance moved = time * speed.
         var distCovered = (Time.time - startTime) * speed;
         
         // Fraction of journey completed = current distance divided by total distance.
         var fracJourney = distCovered / journeyLength;
         
         if(Input.Get$$anonymous$$ouseButtonDown(0)){
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit)){
         if(hit.transform.name == "NavBar"){
         //Zombie.transform.LookAt(hit.point);
         // Set our position as a fraction of the distance between the markers.
         Zombie.transform.position = Vector3.Lerp(start$$anonymous$$arker.position, end$$anonymous$$arker.position, fracJourney);
     }
     }
     }
     }
 
avatar image Sundar · Jan 29, 2014 at 01:18 PM 0
Share

As I said you need to understand how Lerp works here is a best answer http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html You can clearly see you are calling Lerp inside an if condition which is called only once hence your object is jumping to the destination. Go through above example answer, if you still having problem I will fix your code.

avatar image hollym16 · Jan 29, 2014 at 01:32 PM 0
Share

I get the idea of it, it's just implementing it that I'm having trouble with. I'm fairly new to coding and struggle with these more complex code functions. If you could give me a hand with this code I can go through it thoroughly and pick it to bits. Thank you!

$$anonymous$$y current code is as follows:

 #pragma strict
 
 private var ZombieAnim : GameObject;
 private var ZombieIdle : GameObject;
 var transform : Transform;
 var Zombie : GameObject;
 private var ray: Ray;
 private var hit : RaycastHit;
 
 
 function Start () {
 
 ZombieAnim = GameObject.Find("ZF_idle");
 ZombieIdle = GameObject.Find("ZF_idle");
 
 }
 
 function Update () {
 
 if(Input.Get$$anonymous$$ouseButtonDown(0)){
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit)){
         if(hit.transform.name == "NavBar"){
         Zombie.transform.LookAt(hit.point);
         Zombie.transform.position = hit.point;
         ZombieAnim.animation.CrossFade("Walk Legacy", 0.5);
         ZombieIdle.animation.CrossFadeQueued("Idle Legacy", 0.4, Queue$$anonymous$$ode.CompleteOthers);
 }
 }
 }
 }
 

1 Reply

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

Answer by Sundar · Jan 30, 2014 at 12:43 PM

Try this, used 0.1 for Lerp time, change it as required to match your speed. This will give you some basic understanding of how Lerp works. For speed slowing down at end read above link answer where Eric5h5 explained why it happens and how can you avoid it. Good luck coding.

 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.LookAt(hit.point);
                     pointToGo = hit.point;
                     //Zombie.transform.position = hit.point;
                     ZombieAnim.animation.CrossFade("Walk Legacy", 0.5);
                     ZombieIdle.animation.CrossFadeQueued("Idle Legacy", 0.4, QueueMode.CompleteOthers);
                }// if
 
          }// if
 
      }// if
 
      Zombie.transform.position = Vector3.Lerp( Zombie.transform.position, pointToGo, 0.1);
 
 }
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

19 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

Related Questions

Minor Addition to Jumping using CharaterController 1 Answer

Animation Question with PlayMode 1 Answer

Stop animation when no more bullets 1 Answer

animation.Play not affect specific bone 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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