I’m making a game wherein the player, upon spawning, is placed in a room with two objects. The idea is that pressing “e” while looking at one or another will allow the player to teleport to a certain location. Yet the input doesn’t cause a reaction. How may I rectify this?
var targetRed: GameObject;
var redSpawn : Vector3;
var targetBlue : GameObject;
var blueSpawn : Vector3;
private var hit: RaycastHit;
function Update () {
if ( Input.GetKeyDown(KeyCode.E) == true) {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 10)) hit.transform == targetRed;
Vector3.localPosition = redSpawn;
if (Physics.Raycast (transform.position, fwd, 10)) hit.transform == targetBlue;
Vector3.localPosition = blueSpawn;
}
}
Hi Arminius!
You have some simple mistakes in your script. First, I am not sure if this script does what you really want to do. If you want more than this one teleport, you should rewrite your script so every teleporter handles him self.
Your script don’t work because your Raycasts aren’t correctly seted up.
Try this:
var targetRed: GameObject;
var redSpawn : Vector3;
var targetBlue : GameObject;
var blueSpawn : Vector3;
private var hit: RaycastHit;
function Update () {
if ( Input.GetKeyDown(KeyCode.E) == true) {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, hit, 10)) {
if (hit.transform == targetRed.transform)
this.transform.localPosition = redSpawn;
if (hit.transform == targetBlue.transform)
this.transform.localPosition = blueSpawn;
}
}
}
But as I have written, I don’t think this is the best solution for your problem. First, I would use position and not localPosition. The next would be to rewrite your script so every teleporter handles it self. Se OnMouseOver in the documentation. I am more the C# person but this message exists in JS as well, I think. (If not, it would be a great inconsistance in Unity
) I hope I have helped you.