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 Curio · Feb 21, 2012 at 10:20 PM · c#arrayfindwaypointnearest

Moving a GameObject (camera) forward and backward along a series of points (C#).

Hello Answerers!

N.B. I am a noob at scripting & coding, my sincerest apologies.

I have a series of locations that I have specified with gameobjects, I have a simple system whereby I move the camera to a specific location thus:

 using UnityEngine;
 using System.Collections;
 
 public class MoveCameraTo1 : MonoBehaviour {
 
     void OnMouseDown() {
         Camera.main.transform.position = GameObject.Find("pos1").transform.position;
         Debug.Log("we're going to Pos1!");
     }

However, I would also like to move the camera forward or backward along this series (formed of pos1,2,3 etc.). Should I be using an array or list, or way-points or something of that nature? I have also seen scripts that try to find the closest object of a type, but seeing as these points will be equally spaced, I think that would be more troublesome, wouldn’t it?

Thanks in advance to anyone who can help!

EDIT! I have been trying to use this, setting the positions to enemies:

 using UnityEngine;
 using System.Collections;
 
 public class Get : MonoBehaviour {
     Transform GetClosestEnemy(Transform[] enemies)
     {
         Transform tMin = null;
         float minDist = Mathf.Infinity;
         Vector3 currentPos = transform.position;
         foreach (Transform t in enemies)
         {
             float dist = Vector3.Distance(t.position, currentPos);
             if (dist < minDist)
             {
                 tMin = t;
                 minDist = dist;
             }
         }
         return tMin;
     }
     void OnMouseDown() {
         Camera.main.transform.position = Get.transform.position;
         Debug.Log("we're going forward!");
 
 }}

But I have likely borked it up; I dont know what tMin is or how to refer to Get in my OnMouseDown function (is it even a function? I just don't know anymore).

EDIT2! Here is the functional code I spliced:

 Transform[] pointlist = new Transform[3];

 void Start()
 {
     for (int n=0; n < pointlist.Length; ++n)
     {
         pointlist[n] = GameObject.Find("pos"+(n+1)).transform;
     }
 }
 Transform GetPoint(Transform[] Places)
 {
     Transform tMin = null;
     float minDist = Mathf.Infinity;
     Vector3 currentPos = transform.position;
     foreach (Transform t in pointlist)
     {
         float dist = Vector3.Distance(t.position, currentPos);
         if (dist < minDist)
         {
             tMin = t;
             minDist = dist;
         }
     }
     return tMin;
 }
 void OnMouseDown() {
     Camera.main.transform.position = GetPoint(pointlist).position;
     Debug.Log("We're going forward!");

}

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
Best Answer

Answer by mpavlinsky · Feb 21, 2012 at 10:39 PM

You don't need to refer to the class name since you're already in a method of the class. You just have to call the GetClosestEnemy function and get the position out of the transform it returns. I guess first you have to get an array of enemy transforms to pick one from. You can do this either by having a public field in your Get class that is a Transform array that you could assign transforms to in the editor or you could get them programmatically from the scene using GameObject.FindGameObjectsWithTag or something like that and then either change the GetClosestEnemy function to accomodate an array of GameObjects instead of Transforms or convert the array to one of Transforms. After you have the array of transforms you call the function like:

 Camera.main.transform.position = GetClosestEnemy(ENEMIES_ARRAY_GOES_HERE).position;
Comment
Add comment · Show 8 · 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 Curio · Feb 22, 2012 at 06:11 PM 0
Share

Thanks so much for replying, but I really don't know enough about coding to execute your instructions (sorry!). I did however find something which I think is similar to your suggestion which can be found here: http://answers.unity3d.com/questions/10986/transform-array-in-javascript.html (I know it says javascript, but the answerer posted c# as well). If I create a transform array similar to the one in the link, then how would I call the transform in my script? Would it be anything like "GetClosestEnemy(waypoints).position" or something like? Thanks again.

avatar image mpavlinsky · Feb 22, 2012 at 07:52 PM 0
Share

Yes the suggestion in your link would work for this. Of course you would have to name the nodes "p#" in your scene where # is the number of the node starting from 1. Then you would call GetClosestEnemy() exactly as you assumed.

I don't particularly like the method suggested in that link because it is sloppy and you need your objects named sequentially among other potential issues, but it should be fine to get you started.

avatar image Julien-Lynge · Feb 22, 2012 at 08:07 PM 0
Share

BTW: You may want to look at the free iTween plugin (http://itween.pixelplacement.com/index.php), which was designed to do exactly what you've described. It builds a curve from a series of points and allows you to move backwards and forwards over that curve.

avatar image Curio · Feb 23, 2012 at 04:35 PM 0
Share

Stupendous! It works; thanks chaps. I posted the final code above so people from the future could have a peruse if they want.

I do have one last query though, but it's purely out of interest; what does Places in

"Transform GetPoint(Transform[] Places)"

refer to? I though it was the tag, but the script works regardless of tags, so a bit of a mystery.

avatar image mpavlinsky · Feb 23, 2012 at 06:01 PM 0
Share

Where are you seeing this function?

In that context Places is just the name it gives the variable sent in so it has an alias when it uses it inside the function and it doesn't need to know the name the variable was called before entering the function. If you're asking what the Places array is supposed to represent it's hard to say because the function name and parameter name aren't particularly telling.

Show more comments

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need an advise, how to find all objects with a specific name and a specific script? 2 Answers

Is there a way to load all objects in multiple folders within a Resource folder into 1 array? 1 Answer

C# Array problems 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