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 elQuirke · Nov 18, 2014 at 09:05 PM · movementarraylisttargetnode

problem moving character between multiple nodes, storing next target and previous target in array and moving between them (JS)

ive been trying to create a script that stores multiple nodes in a list as transforms and allows a player to move between them using the A and D keys, it works fine going through the array forwards with D but i cant seem to get it to move through the array whilst storing the current node and previous node so it moves between them. im pretty new to coding and a lot of this is adapted through various searches ive done trying to solve the problem. heres what i have -

 import System.Collections.Generic;
 import System.Linq;
 
 var wayPoints : List.<Transform>;
 var currentTarget : Transform; 
 var speed : float = 4000;
 var turnSpeed = 40;
 
 function Awake ()
 {
 // creates node array, adds nodes tagged "Node", sets up initial target node
 
     wayPoints = new List.<Transform>();
     AddNodes();
     TargetNode();
 }
 
 function Update () 
 {
 
 // creates target vector to rotate towards on key press
         
     var currentTargetDir = currentTarget.position - transform.position;
     var step = turnSpeed * Time.deltaTime;
     var currentDir = Vector3.RotateTowards(transform.forward, currentTargetDir, step, 0.0);;
 
     if (Input.GetKey("d"))
     {
         transform.rotation = Quaternion.LookRotation(currentDir);
         rigidbody.AddRelativeForce (Vector3.forward * speed * Time.deltaTime);
     }
 
 //    here i want to have it so pressing A rotates the player to the previous node and moves towards it and then continues
 //  going backwards through the array until D is pressed and then it moves forwards though array again.
 //
 //    if (Input.GetKey("a"))
 //    {
 //        transform.rotation = Quaternion.LookRotation(previousTargetDir);
 //        rigidbody.AddRelativeForce (Vector3.forward * speed * Time.deltaTime);
 //    }
 
 //  when player is 2 units from the target node, stop velocity and change target to next in the array
 //
     
     var distance : float = Vector3.Distance(currentTarget.transform.position, transform.position);
         
     if(distance < 2f)
     {
         rigidbody.velocity.x = 0;
         rigidbody.velocity.z = 0;
         TargetNode();
     }
 } 
 
 function AddNodes()
 {
     var nodeArray : GameObject[] = GameObject.FindGameObjectsWithTag("Node");
     
     for (var node : GameObject in nodeArray)
     {
         AddTarget(node.transform);
     }
 }
 
 function AddTarget (node : Transform)
 {
     wayPoints.Add(node);
     wayPoints = wayPoints.OrderBy(function(x){return x.name;}).ToList();
 }
 
 // have tried to store previousTarget by keeping it one step behind next but it only goes back to the last node and then
 // stops
 
 function TargetNode()
 {
     if(currentTarget == null)
     {
         currentTarget = wayPoints[0];
     }
     else
     {
         var next : int = wayPoints.IndexOf(currentTarget);
     }
     if(next < wayPoints.Count - 1)
     {
         next++;
     }
     else
     {
         next = 0;
     }
     
     currentTarget = wayPoints[next];
 }

some things ive tried -

  • creating a variable alongside next in TargetNode() that stays one step behind it but it only returns to previous not back through array

  • using next++ or next-- based on diection its moving

  • created two seperate TargetNode() based on direction and/or proximity to previoustarget or current target, one ++ one --

  • duplicating the list and reversing it, linking previous target var to this.

sorry for a long post but ive been going over and over this and i cant get it to acheive the node based movement i want, any suggestions or advice would be appreciated, like i said im new to coding.. please no suggesting iTween or splinecontroller or any anything, ive been through them and it doesnt work how i want it to, thanks in advance.

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

2 People are following this question.

avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

FindClosestEnemy() change target? 1 Answer

How do I return the index of an array of sprites as an int? 1 Answer

How to see what element is the current one 2 Answers

Problem with List and Array? 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