So i’m trying to get an object to move from point A to B and C. The script i’m using I adapted from a Java and turn it to C#. Everything seems fine and I was going to test it for logic errors, but I have an error on line 28,25 or the “switch(currentTarget)” line. I’m not sure what the problem might be but I was wondering if anyone has pointers. Also here’s a link to the script I used, the post by BLF_Games.0
using UnityEngine;
using System.Collections;
public class MoveTest2 : MonoBehaviour
{
public Transform targetA;
public Transform targetB;
public Transform targetC;
private Transform currentTarget;
public float proximity = 1f;
public float speed = 1f;
// Use this for initialization
void Start ()
{
currentTarget = targetA;
}
// Update is called once per frame
void Update ()
{
Vector3 Distance = currentTarget.transform.position - transform.position;
if(Distance.magnitude < proximity)
{
switch(currentTarget)
{
case targetA:
currentTarget = targetB;
break;
case targetB:
currentTarget = targetC;
break;
}
}
transform.position = Vector3.Slerp(transform.position, currentTarget.transform.position, Time.deltaTime * speed);
transform.LookAt(currentTarget);
}
}
If you posted the entire error, this may have been a tiny bit easier.
Anyways, I had to Google the error, and it’s a compiler error:
The problem here is a sharp difference between UnityScript and C#: implicit conversions and casts. I’m going to assume you’ll have to do a cast, but as I don’t have much experience doing casts/conversions, I’m going to assume it has something to do with “typeOf(Transform)varnamehere as Transform”, although I doubt that is how it exactly should be.
In other words? Let’s see someone else with more experience in this topic comes in
Edit:
Alternatively, an easier answer would be to compare the transform.position (as I’m assuming the compiler won’t get confused in a Switch statement with a Vector3)
(Also, I said something about the Scripting section- I blame my brain being fried)
Thanks for the feedback James, I did what you recommended and changes it to an “else if” list for the order in which it goes to objects, this worked perfectly. Thanks again for your time.