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 deepak12f · Feb 06, 2016 at 08:39 PM · move an objectmovetowardspathfollowing

How to move an Object in a path?

Hi All,
What I am trying here is, to move a GameObject in a particular path,
I have a straight line of path(in form of cubes), I just want my Player object to travel from one cube point to another in a path.
For Example :
Consider my player is in Cube-2, one pressing two, he should move two more blocks and go to Cube-4, so his path is, [ Cube-3, Cube-4].

The below is my code, but the problem here is, my player object is only moving a little from his current position.
Please help me on this, what am I doing wrongly?

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     
     ArrayList path;
     private Animator animator;
     private int currentPosition;
     private string currentAnim;
 
     void Start () {
         animator = GetComponent<Animator> ();
         currentPosition = 1;
         transform.position = GameObject.Find ("Cube-1").transform.position;
         currentAnim = null;
         playAnimation("unarmed_idle");
     }
     
     void FixedUpdate () {
         ArrayList path = CalculatePath ();
         if (path != null) {
             playAnimation("walk");
             // Move the player from one point to other
             foreach(GameObject pathPoint in path)
             {
                 MoveToPosition (pathPoint.transform);
             }
             playAnimation("idle");
         } else {
             playAnimation("idle");
         }
         
     }
 
     /**
      * Calculates the points from current position to end position
      */
     ArrayList CalculatePath(){
         ArrayList path = null;
         string keyPressed = Input.inputString;
         int moveBy = -1;
         switch (keyPressed) {
         case "1":
             moveBy = 1;
             break;
         case "2":
             moveBy = 2;
             break;
         case "3":
             moveBy = 3;
             break;
         default:
             break;
         }
         if (moveBy != -1) {
             path = new ArrayList (0);
             int endPosition = currentPosition + moveBy;
             // Find the GameObjects from current to end position
             for(int i = currentPosition + 1; i <= endPosition; i++){
                 path.Add(GameObject.Find("Cube-" +  i));
             }
             currentPosition += moveBy;
         }
         return path;
     }
     
     void MoveToPosition(Transform target){
 
         Vector3 currentPosition =  transform.position;
         Vector3 targetPosition = target.position;
         transform.position = Vector3.MoveTowards (currentPosition, targetPosition, Time.deltaTime * 2f);
 
     }
 
     void playAnimation(string animation){
         if (currentAnim != animation) {
             currentAnim = animation;
             animator.Play(currentAnim);
         }
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

39 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

Related Questions

Javascript Script for Object Follow? 0 Answers

How to create Curve between two points using **Vector3.MoveTowards**? 0 Answers

Have an object move towards another object until it hits a certain spot. 1 Answer

NPC Movement 0 Answers

Move Character to touched Position 2D (Without RigidBody and Animated Movement) 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