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 Valdemars Magone · Apr 05, 2011 at 10:00 AM · waypointsarrowguide

Simplifying C# script for guide arrow waypoints

I have an arrow above Player's head to guide him to the exit of level. There are 13 waypoints to avoid arrow looking in only one direction. Also arrow has a script to collide with waypoints and switch to next waypoint. I'm wondering if there is some other way to write these functions, because this is veeeery long... Maybe you have some ideas to make it more simple.

Thanks!

using UnityEngine; using System.Collections;

public class arrowScript : MonoBehaviour {

public bool wp2 = true; public bool wp3 = false; public bool wp4 = false; public bool wp5 = false; public bool wp6 = false; public bool wp7 = false; public bool wp8 = false; public bool wp9 = false; public bool wp10 = false; public bool wp11 = false; public bool wp12 = false; public bool wp13 = false; public Transform wayp2; public Transform wayp3; public Transform wayp4; public Transform wayp5; public Transform wayp6; public Transform wayp7; public Transform wayp8; public Transform wayp9; public Transform wayp10; public Transform wayp11; public Transform wayp12; public Transform wayp13;

void OnTriggerEnter (Collider col) { if (col.tag == "wp1") { wp2 = true; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp2") { wp2 = false; wp3 = true; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp3") { wp2 = false; wp3 = false; wp4 = true; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp4") { wp2 = false; wp3 = false; wp4 = false; wp5 = true; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp5") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = true; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp6") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = true; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp7") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = true; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp8") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = true; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp9") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = true; }

if (col.tag == "wp10") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = true; wp12 = false; wp13 = false; } if (col.tag == "wp11") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = true; wp13 = false; } if (col.tag == "wp12") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = true; } }

void Update (){ if(wp2){ transform.LookAt(wayp2); } if(wp3){ transform.LookAt(wayp3); } if(wp4){ transform.LookAt(wayp4); } if(wp5){ transform.LookAt(wayp5); } if(wp6){ transform.LookAt(wayp6); } if(wp7){ transform.LookAt(wayp7); } if(wp8){ transform.LookAt(wayp8); } if(wp9){ transform.LookAt(wayp9); } if(wp10){ transform.LookAt(wayp10); } if(wp11){ transform.LookAt(wayp11); } if(wp12){ transform.LookAt(wayp12); } if(wp13){ transform.LookAt(wayp13); } }

}

Comment
Add comment · Show 1
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 Kacer · Apr 05, 2011 at 10:24 AM 0
Share

I guess you can do this with a hashtable of some kind, but im kinda horrible at making those :/

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KeithK · Apr 05, 2011 at 10:23 AM

Have your Waypoint object store it's nextWaypoint and call a function on your arrow to set it's current waypoint.

EDIT: Changed as per Bunny's comment.

So in your script on your Waypoint object:

public Transform nextWaypoint = null; // Can drag the next waypoint from the scene editor.

void OnTriggerEnter(Collider TheCollider) { if (TheCollider.gameObject.tag == "arrow") // May want to make sure it's the arrow. { TheCollider.gameObject.GetComponent<ArrowScript>().SetCurrentWaypoint(nextWayPoint); } }

Then in your ArrowScript of your Arrow:

private Transform _currentWaypoint = null;

public void SetCurrentWaypoint(Transform Waypoint) { _currentWaypoint = Waypoint; }

void Update() { if (_currentWaypoint != null) { transform.LookAt(_currentWaypoint); }
}

Replace all occurrences of "ArrowScript" in my example here with whatever the name of the script attached to your Arrow is.

Comment
Add comment · Show 1 · 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 Bunny83 · Apr 05, 2011 at 10:32 AM 1
Share

In your case the arrow will be rotated towards the target, but it won't update when you move around. You should call the function SetCurrentWaypoint or something like that and save the current Transform inside the Arrow script. The lookat should be in Update() or at least called frequently.

avatar image
0

Answer by Owen-Reynolds · Apr 05, 2011 at 04:35 PM

Read a little about arrays. All languages have them, and they all work about the same. The general idea is 13 vars turn into one var with 13 parts. 13 ifs turn into one "lookup by number." A general (Unity-style) setup is:

// In the Inspector, click the arrow and enter the size. Slots will appear: public Transform[] Wayp2;

// if you have 13 slots, they are numbered 0 to 12, so 0 is the 1st waypoint: int curWp = 0; // NOTE: curWp completely replaces that list (wp#) of 13 T/F

// How to get current target: Vector3 walkHere = Wayp[curWp].position; // Ex: if curWp is 2, this looks up Wayp[2], which is the 3rd spot in the list // NOTE: this completely replaces that 2nd set of 13 ifs

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

No one has followed this question yet.

Related Questions

Waypoint System 1 Answer

Is this an [optimal, adequate, ftarded] way to make a snake follow its head? 2 Answers

assign waypoints on a prefab 1 Answer

Waypoint tree structure 2 Answers

Android 2d movement/waypoint script issues 0 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