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 Alter · Jan 31, 2013 at 02:13 PM · waypoint

Pathfinding

Hi guys, I've been following Burgzergs tutorials online and have been focusing on the enemy follow scripts. What I'm trying to achieve is once the 'player' hits the 'target' (Node A) position they will move to the next (Node B), and so on and so on up to at least 10 nodes.

 using UnityEngine;
 using System.Collections;
 
 public class MoveTo : MonoBehaviour {
     
 //    private Transform myTransform;
     public Transform target;
     public float moveSpeed;
     public int rotationSpeed;
     
     public float jumpHeight;
     public int jumpCount = 0;
     
     private Transform myTransform;
     
     void Awake()
     {
         myTransform = transform;
     }
     
     //public GameObject ObjectA;
     //public GameObject ObjectB;
     
     public float smooth = 5.0F;
     
     void Start()
     {
         GameObject go = GameObject.FindGameObjectWithTag("PathNode");
         
         target = go.transform;
             
     }
 
     // Update is called once per frame
     void Update () 
     {
         //bool grounded;
         
         if (target) {
             float dist = Vector3.Distance(target.position, transform.position);
             print("Distance to Node: " + dist);
         
         Debug.DrawLine(target.transform.position, myTransform.position, Color.red);
         
         //Look at target
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
         
         //MoveTo towards target
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
                 
         }
     }
     
     public static float RelativeTurnAngle(Transform turn, Vector3 target) {    
         // determine the angle
     Vector3 relative = turn.InverseTransformPoint(target);
     return Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
     }
             
             
     }

I have also tried to have a list system but cannot wrap my head around it.

Comment
Add comment · Show 9
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 deltamish · Jan 31, 2013 at 04:26 PM 0
Share

what are the problem you are facing?

avatar image Alter · Jan 31, 2013 at 04:32 PM 0
Share

I'm pretty much trying to get from one 'point' to another- If it makes it easier i'm working on a runner game and want the player to run a track on it's own but with controls. eg: move left and right with jump while going towards the next 'point'.

avatar image Alter · Jan 31, 2013 at 04:34 PM 0
Share

I found this script but I can't get the player to move and it's in java- i'm new to program$$anonymous$$g and still have issues converting to C#.

 var target : Transform[];
 var timer : float = 0.0;
 var rate : float = 5.0;
 private var currentTarget: int;
  
 function FixedUpdate () {
 timer += Time.deltaTime;
     if(timer > rate){
     this.GetComponent("Player").StartPath(transform.position,target[currentTarget].position);
     timer = 0;
     }
     if(Vector3.Distance(transform.position,target[currentTarget].position)<=0){
         currentTarget++;
         if(currentTarget>=target.Length){ 
         currentTarget = 0;
         }
   
avatar image deltamish · Jan 31, 2013 at 04:45 PM 0
Share

Okay first I in your first script i think i didn't see anywhere you defining wether the player is close to a path node or not

Secondly

In your second script I believe StartPath is another function and not a built in fearure

So here is the conversion

       //Omitted Using UnityEngine and others
         public Transform[] target;
         public float timer = 0.0f;
         public float rate = 5.0f;//you should use f at the end of num in C#
         Transform _transform;
 int currentTarget;
         void Awake(){
         _transform = transform;//Cache it for performance
         }
         void FixedUpdate(){
         timer += Time.deltaTime;
         if(timer > rate){
         _transform.GetComponent<Player>().StartPath(_transform.position,target[currentTarget].position);
         
       
     timer = 0;
     }
     if(Vector3.Distance(_transform.position,target[currentTarget].position)<=0){
     currentTarget++;
     if(currentTarget>=target.Length){
     currentTarget = 0;
     }
     }
     }
      
avatar image Alter · Jan 31, 2013 at 05:03 PM 0
Share

Cheers for this, but is there a way to move the player here without using the 'StartPath'?

Also should _transform = Transform; be _transform = transform? 'cos it come up with an error :(

Show more comments

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

12 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

Related Questions

question on using waypoints and an arrow to guide a player. 2 Answers

Waypoint NullReferenceException: Object reference not set to an instance of an object error please help. 0 Answers

Create "WayPoint" object in editor 1 Answer

Enemy detection while pathfinding through the map 1 Answer

Matching Waypoint Rotation 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