Unity как transform преобразовать в vector

Обновлено: 13.05.2024

So I want to instanstiate an object and update its position from what where I am spawning it. I am spawning this object on the parets + 10x position.

When I do this, it nearly does what I want it to, however it will spawn the object and give it 10x on the global position not local, where it was spawned from. After looking online, I tried:

However that gave me the error of "CS1061: 'Transform' does not contain a definition for 'localpositon'"

Would anyone know what I am missing when it comes to instanstiating something on a local Vector3?

Edit* Thinking of the terminology, I guess the result that I am expecting is for the object I am creating to be pivoted around a certain point when spawned in, not just rotated and position on the Vector3 coordinates.

This point being the spawn location in the world, which is just parent world location + child location.

Transform

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Unity превращение transform.rotation в Vector2

Когда игрок входит в триггер, идёт проверка на булл, в зависимости от которого, задаётся новый вектор. Сейчас понял, что это можно оптимизировать, но мозга не хватает понять как. Как, к примеру, сделать, что бы при входе игрока в коллайдер, задавался вектор в зависимости от поворота коллайдера?

Объясните нормально чего вы хотите. Из вопроса не очень понятно. Если вы хотели чтобы вектор указывал вперед просто умножьте Quaternion вращения на требуемый вектор, это приведет к повороту. transform.rotation * Vector3.forward

Сложно объяснять то, что сам не очень понимаешь, но да, это именно то, что нужно)

2 Answers 2

Your issue is explained very well by the error you're receiving, lets have a look

"Cannot implicitly convert type 'UnityEngine.Vector3' to 'UnityEngine.Transform'"

So where are you trying to set a Transform 's value to a Vector3?

spawningpos is a Transform

But here you are clearly setting its value to a Vector3

On this line you then instantiate the object - using the Transform as input. As you will find here, the overload you are using for the method is actually asking for the parent's Transform, try this instead

Followed by, as before

This code will call the correct overload of the Instantiate method, setting the object's rotation is mandatory when passing a Vector3

convert vector3 to transform

What is the most effective way to convert a Vector3 to Transform?


110 1 1 gold badge 3 3 silver badges 13 13 bronze badges "spawningpos.position = new Vector3(Random.Ran. " but this is not the right way to do stuff. EDIT: see the answer below why and how :)

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Your name Your email Suggestion * Submit suggestion

Description

Position, rotation and scale of an object.

Every object in a Scene has a Transform. It's used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. This is the hierarchy seen in the Hierarchy pane. They also support enumerators so you can loop through children using:

Читайте также: