- Home /
Connecting transform with Vector3
Hello all,
I know this is a much researched question, but after looking for many answers, here it is: I have an array of points stored as Vector3:
[HideInInspector] public Vector3 [] waypoints;
[SerializeFieald] private Transform[] waypointsTransform;
private void InitializeWaypoints()
{
waypoints = new Vector3 [waypointsTransform.Lenght];
for (int i = 0, i < waypointsTransforms.Lenght; i ++)
{
waypoints[i] = waypointsTransforms[i].position;
}
After initialized, I'm calling its rotation, for the AI's script:
private void TurnToShelf()
{
Transform pointRotation
pointRotation.rotation = SceneIndex.instance.waypoints [nextWayPoint];
float turningSpeed = 5.0f;
transform.rotation = Quaternion.Lerp(transform.rotation, pointRotation.rotation, turningSpeed * Time.deltaTime);
}
Here I got errors like "cannot convert .... vector3 to Quaternion
I just want the AI to face the x direction of the waypoint.
I couldn't solve but I found a mistake that you wrote [SerializeFieald] ins$$anonymous$$d of [SerializeField] in this line : [SerializeFieald] private Transform[] waypointsTransform;
yes, I was writing the code by hand from another computer... it's ok originally. thanks.
Answer by Vicarian · Jun 12, 2017 at 01:57 PM
pointRotation.rotation = SceneIndex.instance.waypoints [nextWayPoint];
The above line is the problem. You're attempting to assign a Vector3 to a Quaternion. You could change the line to
pointRotation.rotation = Quaternion.Euler(SceneIndex.instance.waypoints[nextWayPoint]);
but I would change the waypoints collection to a collection of Transforms instead. That way you get both position and direction stored.
Answer by Eco-Editor · Jun 12, 2017 at 05:24 PM
I've changed the collection to be of Transforms
[SerializeField] private Transform[] waypointTransforms;
[HideInInspector] public Transform[] waypoints;
private void InitializeWaypoints()
{
waypoints = new Transform[waypointTransforms.Length];
for (int i = 0; i < waypointTransforms.Length; i++)
{
waypoints[i] = waypointTransforms[i];
}
}
And in AI's script, it's Quaternion.Lerp:
Transform pointRotation;
private void TurnToShelf()
{
turningSpeed = 5.0f;
pointRotation = SceneIndex.instance.waypoints[nextWayPoint];
shopper.transform.rotation = Quaternion.Lerp(shopper.transform.rotation, pointRotation.rotation, turningSpeed * Time.deltaTime);
}
It's not working for me...
Is there an error or is the AI simply not doing anything?
Simply not doing anything, but the Debug.Log says it does. I have the same function on the other AI script and it's working.
The other function initializes the way points in a different way:
private void InitShelfPoints()
{
if (transform.childCount > 0)
{
_shelfPoints = new ShelfPoint[transform.childCount];
for (int i = 0; i < _shelfPoints.Length; i++)
{
_shelfPoints[i] = new ShelfPoint(transform.GetChild(i));
}
}
So you have a transform holding all the shelves your AIs visit. What's the other method doing:? There isn't much to go on with it. With a Serialized collection, you can actually assign the points (transforms) by dragging the object with that transform from the Hierarchy to the Inspector, so you wouldn't actually need a loop in that case.
Answer by Raimi · Jun 12, 2017 at 05:36 PM
obj.transform.Rotate (new Vector3 (0, 0, GameManager.instance.Speed))
or
void Update()
{
obj.transform.Rotate( new Vector3 (0, 0, GameManager.instance.Speed));
}
or
void Start()
{
StartCoroutine(RotateNow());
}
IEnumerator RotateNow()
{
while(true)
{
obj.transform.Rotate( new Vector3 (0, 0, GameManager.instance.Speed));
yield return null;
}
}
This Probably doesn't fix your issue, but it might help give you some ideas :)
Look, you're assigning the float z to be:
Game$$anonymous$$anager.instance.Speed
while in my case: SceneIndex.instance.waypoints[nextWayPoint]));
Is a Vector3 or a Transform but not a float. so I receive an error "cannot convert Vector3 to float"
This is some examples, as I said, it wont fix your issue, but give you some ideas. sorry its not perfect
Answer by Pedro_Brito · Jun 12, 2017 at 11:00 PM
Simple, the collection of waypoints should be of type Transform. Then, where you access waypoints at the AI script, you should access the rotation variable of the transform. In this case, there's no need to store 2 collections. You just need one.
Hi, where do I store 2 collections? Do you mean here: [SerializeField] private Transform[] waypointTransforms; [HideInInspector] public Transform[] waypoints;
?
I've removed the other collection, and changed it to:
private void InitializeWaypoints()
{
for (int i = 0; i < waypoints.Length; i++)
{
waypoints[i] = GetComponent<Transform>();
}
But after assigning the waypoints in the inspector, I don't have the avatar going to neither one of them, but rather go to some random place and stay there.