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 KyleFN · Jul 30, 2012 at 11:40 PM · movementnpcfollowing

How to make an object follow on 1 axis??

Howdy:

I'm taking a scripting class this semester and I need some help. I've been researching now for about half an hour and I can find a lot of questions that are sort of what I need, but not exactly, and I can't seem to successfully adapt any of the answers to what I'm working on.

I'm making a side scrolling "endless runner". It scrolls right to left and you control a character in the center that can move forward & backward 3 spaces and left & right 5 spaces (relative to the character). You are being chased by an enemy (both the enemy & the character are simple cubes at this point) that follows behind you and follows your movement along the Z coordinate.

alt text

I've kinda sorta got it working but I think there's a better solution out there and would like so suggestions. I've tried understanding Vector3.Lerp & Vector3.MoveTowards, but I can't make them do what I need them to do.

Let me preface all of this by saying that I just started working with Unity this semester so I'm a complete newbie at all this, and have limited programming skills (took an introductory C++ class last semester), so please bear that in mind when replying.

Here's the following cube's code that "sorta works":

 #pragma strict
 
 function Start () 
 {
 
 }
 
 var IceKingSpeed = 20; // IceKing's movement speed
 //var IceKingDelay = 0.5; // The amount of time (in seconds) the IceKing delays before following Finn
 var zIceKing : float;
 var IceKingzPosMoveLimit = 15;
 var IceKingzNegMoveLimit = 3;
 var IceKingKnockBack = 0.01;
 var target : Transform;
 var smooth = 5.0;
 
 function Update () 
 {
 //  Debug.Log("IceKing's z Position => [" + zIceKing + "] & IceKing's x Position => [" + xIceKing + "]"); // Shows the z position of Finn
 //  Basic Tracing Movement
  zIceKing = transform.position.z;
  var z = Input.GetAxis("Vertical") * Time.deltaTime * IceKingSpeed;
  if (zIceKing <= IceKingzPosMoveLimit && zIceKing >= IceKingzNegMoveLimit)
      {
 // yield WaitForSeconds(IceKingDelay); ==> Never could this damn delay thing to work right, so I took it out.     
  transform.Translate(0, 0, z);
 // transform.position.z = Vector3.Lerp (transform.position, target.position, Time.deltaTime * smooth); ==> I tried this but ERROR
  }
     else if (zIceKing > IceKingzPosMoveLimit)
         transform.position.z -= IceKingKnockBack;
     else
      transform.position.z += IceKingKnockBack;         
 }

I'm open to suggestions, but please save the criticism if you have nothing constructive to offer, I'm trying to learn.

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

Answer by whydoidoit · Jul 31, 2012 at 12:04 AM

So you could do this:

  • Define the target position of the ice king as a script level variable

  • Change that position based on your input

  • Clamp the position

  • Lerp to that position for smooth movement

       var iceKingTargetPosition : Vector3;
         var smoothingFactor : float = 4; //Smaller is more smooth
         var IceKingSpeed = 20; // IceKing's movement speed
         var IceKingzPosMoveLimit = 15;
         var IceKingzNegMoveLimit = 3;
    
         function Start() {
             iceKingTargetPosition = transform.position;
         }
    
         function Update() {
              iceKingTargetPosition.z = Mathf.Clamp(iceKingTargetPosition.z + Input.GetAxis("Vertical") * Time.deltaTime * IceKingSpeed,
                      IceKingzNegMoveLimit, IceKingzPosMoveLimit);
              transform.position = Vector3.Lerp(transform.position, iceKingTargetPosition, Time.deltaTime * smoothingFactor);
         }
    
    

The only problem with this is that it will take a long time for the character to actually reach the target z (although it will close in quickly) as you are always moving towards it by a fraction.

You could also do a SmoothDamp - which will be more rapid to get there. The key to this technique is that the iceKingTargetPosition is a real world position.

     var iceKingTargetPosition : Vector3;
     var smoothingFactor : float = 0.5; //Smaller is less smooth
     var IceKingSpeed = 20; // IceKing's movement speed
     var IceKingzPosMoveLimit = 15;
     var IceKingzNegMoveLimit = 3;
     var IceKingVelocity : Vector3;

     function Start() {
         iceKingTargetPosition = transform.position;
     }

     function Update() {
          iceKingTargetPosition.z = Mathf.Clamp(iceKingTargetPosition.z + Input.GetAxis("Vertical") * Time.deltaTime * IceKingSpeed,
                  IceKingzNegMoveLimit, IceKingzPosMoveLimit);
          transform.position = Vector3.SmoothDamp(transform.position, iceKingTargetPosition, iceKingVelocity, smoothingFactor);
     }
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

Simple movement for NPC C# 0 Answers

Question, Need Help! 1 Answer

Help me: Wasd movement, Model following my mouse 0 Answers

How to check if a character hasn't changed its position in the last x seconds? 1 Answer

Mecanim Root Motion -- Not Moving Forward? 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