- Home /
Waypoint Arrow not pointing at tagged object ?
Hi ,strange problem here as usual lol ,I am using a simple piece of code which is attached to a cone mesh which is a child of the main camera and I want it to point at my tagged waypoints in turn ,so I start simple and use waypoint 1 (tagged as "1") and the arrow rotates but actually points at the center of my terrain and not the tagged waypoint ? (I have checked and the terrain is not tagged at all) ? heres the JS (pragma strict) . Any clue ?
#pragma strict
function Update () {
var object = GameObject.FindGameObjectWithTag("1");
var target = object.transform;
transform.LookAt(target , Vector3(0,0,1));
transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);
}
lol fixed ,I had a DUH moment after I posted ,the Original code was wrong the line
transform.LookAt(target , Vector3(0,0,1));
should be
transform.LookAt(target);
Hope this helps people with arrow pointer problems lol :)
As im here already ^^ I am trying to read the value of ckp from a script called Checkpoint01 so I know where the arrow should point but I guess I have the example wrong .Any ideas please ,I should point out that the scripts are on two different objects :()
#pragma strict
function Update () {
// Find the OtherScript which is attached to any game object in the scene.
var other : Checkpoint01 = FindObjectOfType(ckp);
if (ckp==1) var object = GameObject.FindGameObjectWithTag("1");
if (ckp==2) var object = GameObject.FindGameObjectWithTag("2");
var target = object.transform;
transform.LookAt(target);
transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);
}
Answer by robertbu · Apr 02, 2013 at 03:21 PM
FindObjectOfType() is very slow and should not be done every frame. Typically you would give the game object with the script attached a name know name and use GameObject.Find() to get the object in start (or drag and drop into a public variable). I also don't see where ckp is declared or set in this code, so I cannot check how that is being used. And you execute code even if object has not been initialized. Here is your code with a couple of mods:
#pragma strict
var other : CheckPoint01;
function Start () {
other = GameObject.Find("Other_Name").GetComponent(CheckPoint01);
}
function Update () {
var object : GameObject = null;
if (other.ckp==1)
object = GameObject.FindGameObjectWithTag("1");
if (other.ckp==2)
object = GameObject.FindGameObjectWithTag("2");
if (object != null) {
var target = object.transform;
transform.LookAt(target);
transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);
}
}
I assume the transform.rotation is because the natural rotation of the arrow is not towards positive 'z'?
var ckp =0;
this is setup in CheckPoint01.js
ps what do I put in other_name field ,sorry im a bit lost still lol
I get this error ?
Assets/direx.js(3,13): BCE0018: The name 'CheckPoint01' does not denote a valid type ('not found').
Is it because the checkpoint script is not on the same object ? and can I get a value from a script on a different object ?
I edited the code above to access ckp in other. "Other_Name" will be the name of the game object that the CheckPoint01 script is attached to. As for the error, the type and the file name must match exactly. That is "Checkpoint01.js" is not "CheckPoint01.js" If the names match, then the error should go away.
As I said above, the names must match exactly. So you can either change the name of the script to "CheckPoint01", or you can change the type used above to "Checkpoint01". Even a capital letter difference is enough to generate the error.
Still problems lol heres my ckp value in the Checkpoint01 script
// Checkpoint01.js
if (hit.gameObject.name == "Cube1"){
ckp=ckp+1;
GameObject.Find("Cube2").transform.Translate(0, -5000, 0);
GameObject.Find("Cube1").transform.Translate(0, 5000, 0);
}
else if (hit.gameObject.name == "Cube2"){
ckp=ckp+1;
GameObject.Find("Racetext").renderer.enabled = false;
GameObject.Find("Cube3").transform.Translate(0, -5000, 0);
GameObject.Find("Cube2").transform.Translate(0, 5000, 0);
}
else if (hit.gameObject.name == "Cube3"){
ckp=ckp+1;
GameObject.Find("Cube4").transform.Translate(0, -5000, 0);
GameObject.Find("Cube3").transform.Translate(0, 5000, 0);
}
I just need to read the value of ckp from the above script into this one below
// direx.js
#pragma strict
function Update () {
if (ckp==1) var object1 = GameObject.Find("Cube2");
var target1 = object1.transform;
transform.LookAt(target1);
transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);
if (ckp==2) var object2 = GameObject.Find("Cube3");
var target2 = object2.transform;
transform.LookAt(target2);
transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);
// etc till
if (ckp==10) var object3 = GameObject.Find("Cube1");
var target3 = object3.transform;
transform.LookAt(target3);
transform.rotation = transform.rotation * Quaternion.Euler(90,0,0);
}
Your answer
Follow this Question
Related Questions
FindGameObjectsWithTag works only to first tagged 1 Answer
Swapping a tag for string not working, replacing a tag with a public string, help. 1 Answer
Using tags to move player toward objects 1 Answer
Get a total count of gameobjects with the same tag and change level if score reaches max score! 0 Answers
How to move a gameobject when it lands on a tag, to another tag? 0 Answers