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 Flash · Apr 23, 2012 at 11:36 PM · movementvector3directiontravel

Calculate direction of travel.

Hello everyone.

Before asking my question I'll just tell you a bit about the project I'm working on.

I'm making a MOBA or ARTS game with a few friends for us to play at lans. We've created a map and have all the camera controls working as well as Fog of war.. All that good stuff.

Now the way you move the character/champion you control is by clicking on the point you want to move to. Once you click on the screen a prefab called WalkPoint is instantiated. Your champion will (if it does not already have a reference to the WalkPoint) search for the WalkPoint placed by the client who controls him.

The problem we're having is the champion does not face the direction he's traveling.

Now using transform.LookAt(); is not a possibility since the champion will have Object Avoidance and if he walks into a wall he will start sliding along it thus changing his direction of travel (We're using the Character Controller's Move(); method).

We've already tried several different methods like saving his position into a temp variable before moving him and then calculate the angle between that variable and his new position.

 Vector3 dirOfTravel = (transform.position - oldPos).normalized;

Without luck. What happens using these methods is that he simply doesn't turn to face the direction we calculate or he bugs out and screws up the Move(); method (Making him walk the wrong way or rotate rapidly while moving up on the Y axis).

Here's the current base for our champions if it is of any help.

 using UnityEngine;
 using System.Collections;
 
 public class Champ_Base : MonoBehaviour {
     
     public float WalkSpeed = 5.0f;
     public float Gravity = 20.0f;
     
     private Transform WalkPoint = null;
     private CharacterController cc;
     
     void Start(){
         cc = GetComponent<CharacterController>();    
     }
     
     void Update () {
         //Get a walk point to move to.
         if(WalkPoint == null){
             GameObject[] walkpoints = GameObject.FindGameObjectsWithTag("walkindicator");
             if(walkpoints.Length > 0){
                 WalkPoint = walkpoints[0].transform;
             }
         }
         else {
             //Move towards the walkpoint.
             Vector3 moveDir = (WalkPoint.position-transform.position).normalized;
             moveDir = transform.TransformDirection(moveDir);
             moveDir *= WalkSpeed;
             cc.Move(moveDir*Time.deltaTime);
             
             //Remove the walk point once we get close enough.
             float disToPoint = Vector3.Distance(transform.position, WalkPoint.position);
             if(disToPoint < 1.25f){
                 Destroy(WalkPoint.gameObject);
                 WalkPoint = null;
             }
         }
         
         //DEBUG FACING DIR.
         Debug.DrawRay(transform.position, transform.forward, Color.red);
     }
     
 }

(Please note that there is no turning to face direction in the code above)

I appriciate you talking your time to read this and I hope someone can explain a way to calculate the direction of travel.

Thanks.

Comment
Add comment · Show 5
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 numberkruncher · Apr 24, 2012 at 02:22 PM 0
Share

Here are a couple of notes. (1) are you absolutely sure that your model is facing towards the local Z axis of the game object? (2) what happens if you translate using the unit vector transform.Translate(moveDir * walkSpeed, Space.World);? I have not put this as an answer because I do not entirely understand the question...

avatar image Flash · Apr 24, 2012 at 02:42 PM 0
Share

Yes I'm completly sure my character is facing the Z axis.

If I use transform.Translate(); he still moves in the right direction. The reason I'm using the $$anonymous$$ove(); method of the Character Controller is so that the Character takes collisions into consideration when moving.

I'm not using transform.forward to move him along his facing direction since I want his movement (Towards the WalkPoint) and his rotation on Y (The direction he's facing) to be seperate so that he may face the direction he's travelling when for example sliding along a wall (Collider).

What I need is a way for me to calculate the characters direction of travel (The direction he's moving) no matter how he is rotated.

$$anonymous$$y initial idea was to use Vector3 dirOfTravel = (transform.position - oldPos).normalized; which does not seem to work.

I'm sorry if my problem seems unclear.. I really don't know how I can explain it better. If my question is still unclear I can probably upload the project as a webplayer?

avatar image numberkruncher · Apr 24, 2012 at 02:45 PM 0
Share

A webplayer demo would be useful to understand the issue. Does the problem occur when the player stops and starts? The old position becomes the same as the new position and thus direction vector is zero.

avatar image Flash · Apr 24, 2012 at 03:05 PM 0
Share

Alright.

Here is the build without any code for facing the direction of travel. http://dl.dropbox.com/u/36230178/WorkingWebBuild/WebPlayer/WebPlayer.html (This shows how the character moves around, simply click on the point you want to move to). There is no rotation on the character when moving in this build.

Here is a build where I have added some facing code. http://dl.dropbox.com/u/36230178/NonWorkingWebBuild/WebPlayer/WebPlayer.html Here I save the position of the character before moving him then using that to make him face the direction he is travelling. As you may see he just stands there (moving very little). When I play the game in the editor I can see that he is spinning wildly around his Y axis.

avatar image WobblyBob · Oct 26, 2016 at 01:51 PM 0
Share

What I would do is use the built in character controller. https://docs.unity3d.com/$$anonymous$$anual/class-CharacterController.html

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by irgendeine · Apr 24, 2012 at 01:58 PM

Did you have a look at this: http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt

works fine for my character...

Comment
Add comment · Show 1 · 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 Flash · Apr 24, 2012 at 02:12 PM 0
Share

As I said.. LookAt(); is not a possibility. If I use LookAt() the character will always look at the point he's walking towards (Even if the object avoidance changes his direction of travel or if he slides along a wall) which does not look good at all.

What I need is a way to calculate which direction the character is moving.

avatar image
0

Answer by RevVo98 · Aug 25, 2013 at 07:34 PM

Calculate the position in which the player has to move to the next frame, but before you actually move him to that location make him LookAt the location.

 Vector3 moveTo = transform.forward * Time.deltaTime;
 transform.LookAt(moveTo);
 transform.position += moveTo;

:)

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

Answer by Zodiarc · Oct 26, 2016 at 02:15 PM

I guess you're using some kind of pathfinding, so you should have access to the points in the path. As the character is following the path let him look at the point he is moving to. If the distance is lower than a threshold you define, let him look at the next point in the path and so on till he reaches the destination.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"Fake" Vector3.forward? 3 Answers

iTween Path Editor: ignore Y position in Vector3 1 Answer

How to find the direction to translate an object, to move towards a point, regardless of rotation? 1 Answer

How to make a Direction Indicator to show where and object is going? 1 Answer

How can I move character controller in one direction 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