Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 · Apr 30, 2018 at 07:45 PM · prefabimagespawnvuforiatarget

Vuforia, how to spawn a prefab on target image and not above?

Hello all,

I'm building an AR scene with Vuforia image target. Once I touch the image target the prefab appears but it looks "hanging" in the air. I'd like the prefab to appear as if it is "standing" on the target image, namely so that the prefabs' and the images' y coordinate is the same.

this is my code in C#:

 using UnityEngine;
 
 public class SpawnSystem : MonoBehaviour
 {
 
     public Transform targetObject;
     public Transform targetPlane;
 
     //float distance = 10f;
 
     Animator anim;
 
     private void Awake()
     {
         anim = GetComponentInChildren<Animator>();
     }
 
     private void Update()
     {
 
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
         {
             Vector3 fingerPos = Input.GetTouch(0).position;
             //fingerPos.z = 5;
             Vector3 objPos = Camera.main.ScreenToWorldPoint(new Vector3 (fingerPos.x, fingerPos.y, fingerPos.z));
             targetObject.position = objPos;
             Instantiate(targetObject, targetObject.position, targetObject.transform.rotation, targetPlane);
         }
     }
 }
 
 

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 TreyH · Apr 30, 2018 at 07:49 PM 0
Share

The issue you're having is very common and has been asked / answered quite a bit.
https://docs.unity3d.com/ScriptReference/Transform-position.html

You can't modify position like that because fields of a struct cannot be directly modified. You have to create a new Vector3 instance, assign the x/y/z components, then reassign the objects position entirely.

avatar image Eco-Editor TreyH · Apr 30, 2018 at 10:40 PM 0
Share

Hi @TreyH I've managed to solve this for a while partially... but it still not working. I've reassigned the position:

     public Transform towerPrefabClone;
     public GameObject towerPrefab;
     float floorHeight = 0f;
 
     void Update()
     {
 
         for (int i = 0; i < Input.touchCount; i++)
         {
             if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 Vector3 fingerPos = Input.GetTouch(0).position;
                 fingerPos.z = 5;
                 Vector3 objPos = Camera.main.ScreenToWorldPoint(fingerPos);
                 towerPrefabClone = (Instantiate(towerPrefab, objPos, Quaternion.identity) as GameObject).transform;
                 Vector3 groundPos = new Vector3 (objPos.x, floorHeight, objPos.z);
 
                 if (towerPrefabClone.position.y > floorHeight)
                 {
                     towerPrefabClone.position = groundPos;
                    towerPrefabClone.GetComponent<Animator>().SetBool("RisingAbove", true);
                 }
             }
         }

 

At first y was zero, but now it's not working.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eco-Editor · May 09, 2018 at 03:19 PM

I've solved this by searching further here's the best solution: https://developer.vuforia.com/forum/general-discussion/getting-object-touch-position

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.

Follow this Question

Answers Answers and Comments

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

How to turn a spawned enemy ( AI ) target on player?/ Why are my Hierarchy object turn prefab then no longer hold the transform object? 1 Answer

Give prefab a parent 2 Answers

How can I make an in-game search engine similar to Scribblenauts 1 Answer

i need to destroy prefab clones 1 Answer

Use prefab as particle in particles system 0 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