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 /
  • Help Room /
avatar image
0
Question by AZAXD64 · Jan 24, 2016 at 07:02 AM · javascriptmovementvector3movetowardsdiagonal

How to disable diagonal Vector3 movement of non-player object using MoveTowards?

Hi everyone,

My 3D game has a NPC sprite moving towards it's target object and stopping once it reaches it. I'm after some help on how to prevent the sprite moving diagonally. I understand it must be something like using GetAxis and telling the object to not move on the x-axis if the y-axis has movement but I just can't figure it out exactly. With my player, putting a "return;" after each of it's movement inputs did the job perfectly but I believe that doesn't really apply to this because I'm not specifically addressing each direction of Vector3 movement right?

Apologies for my very amateur coding knowledge. I'd hate to be one of those people on here who asks silly questions and tries to get people to do it for them. I've been stuck on this for a couple hours however and couldn't solve it looking at all the other questions on this lovely site or others...

Here's my code: Thanks a lot :)

 #pragma strict
 
 var target: Transform;    
 var speed: float;
 var thereyet: boolean;
 
 
 function Start(){
     thereyet = false;
     }
         
         
 function OnTriggerEnter (other : Collider) {
     if(other.gameObject.tag=="Position_1"){
     thereyet = true;
     }
     }
 
 function OnTriggerExit (other : Collider) {
     if(other.gameObject.tag=="Position_1"){
     thereyet = false;
     }
     }
     
 function Update(){
     if (thereyet == false){
     var step = speed * Time.deltaTime;
     transform.position = Vector3.MoveTowards(transform.position, target.position, step);
     }
     }
 

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 hexagonius · Jan 24, 2016 at 08:17 AM 0
Share

there's no input, just a move towards

avatar image AZAXD64 hexagonius · Jan 24, 2016 at 10:12 AM 0
Share

Are you saying there's no way to stop the sprite moving diagonally if it's using $$anonymous$$oveTowards?

1 Reply

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

Answer by andrew-lukasik · Jan 24, 2016 at 11:52 AM

One simple solution:

(C#)

 void Update () {

         float step = speed*Time.deltaTime;

         //vector math 101
         //target.position - my.position will give you direction to target position from my position:
         Vector3 directionToTarget = target.position-transform.position;
         
         //Math.Abs() is merely to make every value positive so we can compare lengths regardless of their -+ sings
         //simply put i'm checking here is directionToTarget.x shorter or directionToTarget.y (and not near zero(Mathf.Epsilon is a shorthand for near-zero value))
         //and then setting to zero other direction
         if( Mathf.Abs( directionToTarget.x )<Mathf.Abs( directionToTarget.y ) && Mathf.Abs( directionToTarget.x )>Mathf.Epsilon ) {
             directionToTarget.y = 0f;
         }
         else if( Mathf.Abs( directionToTarget.y )>Mathf.Epsilon ) {
             directionToTarget.x = 0f;
         }

         //my.position + directionToTarget.position gives targetPosition again:
         transform.position = Vector3.MoveTowards(
             transform.position ,
             transform.position+directionToTarget ,
             step 
         );
 
     }

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 AZAXD64 · Jan 24, 2016 at 04:45 PM 0
Share

Thanks mate that's been a huge help! Really appreciate you writing all the info in there too, helps me out big time :) Cheers

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

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

Related Questions

Javascript Script for Object Follow? 0 Answers

How to make object solid with Vector3.MoveTowards? 1 Answer

The object moves more that what I want (MoveTowards) 0 Answers

Objects slowly move by their own? 1 Answer

collision is making its own vector 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