• 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 Eco-Editor · Jun 12, 2017 at 12:47 PM · rotationtransformvector3quaternionwaypoints

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.

Comment
Add comment · Show 2
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 opsfentek · Jun 12, 2017 at 01:30 PM 0
Share

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;

avatar image Eco-Editor opsfentek · Jun 12, 2017 at 03:08 PM 0
Share

yes, I was writing the code by hand from another computer... it's ok originally. thanks.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

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 Eco-Editor · Jun 13, 2017 at 10:58 AM 0
Share

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.

avatar image
0

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 :)

Comment
Add comment · Show 3 · 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 Eco-Editor · Jun 12, 2017 at 06:09 PM 0
Share

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"

avatar image Raimi Eco-Editor · Jun 12, 2017 at 10:23 PM 0
Share

This is some examples, as I said, it wont fix your issue, but give you some ideas. sorry its not perfect

avatar image Eco-Editor Raimi · Jun 13, 2017 at 10:53 AM 0
Share

It did gave me some ideas.

avatar image
0

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...

Comment
Add comment · Show 5 · 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 Vicarian · Jun 12, 2017 at 08:45 PM 0
Share

Is there an error or is the AI simply not doing anything?

avatar image Eco-Editor Vicarian · Jun 13, 2017 at 11:04 AM 0
Share

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));
             }
         }

avatar image Vicarian Eco-Editor · Jun 13, 2017 at 06:16 PM 0
Share

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.

Show more comments
avatar image
0

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.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

101 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform a Vector by a Quaternion 1 Answer

Instantiate GameObject towards player 0 Answers

Make a side of an object LookAT another object 1 Answer

How to smoothly rotate to certain directions using input axis 1 Answer

c# modify only one axis of a quaternion 2 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