• 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 /
This question was closed Apr 24, 2018 at 09:13 PM by strausse1.
avatar image
1
Question by strausse1 · Apr 23, 2018 at 02:06 AM · instantiate prefab

Instantiate rotation related to player rotation

Hello im super new and i´m trying to make a spell that is an earth wall rising from the ground. i managed to google everything so far but i cant get the wall to rotate in relation to my main character. the game is like top down view with an angle im using a navmesh and a navmeshagent to navigate. thats what i got so far

public class EarthWall : MonoBehaviour {

 public float cooldownTime;
 private float nextFireTime;
 
 Ray myRay;
 RaycastHit hit;
 public GameObject objectToInstantiate;

 // Update is called once per frame
 void Update () {
     if (Time.time > nextFireTime)
     {
         myRay = Camera.main.ScreenPointToRay(Input.mousePosition);

         if (Physics.Raycast(myRay, out hit))
         {
             if (Input.GetKeyUp("q"))
             {
                 Instantiate(objectToInstantiate, hit.point, Quaternion.identity);
                 nextFireTime = Time.time + cooldownTime;
             }
         }
     
     }
 }

}

i hope i made my point clear my english is not the best im sorry. thanks for any help i get

as a bonus question, i want the wall to only be casted in a certain range from the player :)

Comment
Add comment · Show 3
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 vir1234 · Apr 23, 2018 at 11:41 AM 1
Share

try this Instantiate(objectToInstantiate, hit.point, player.transform.rotation);

Show more comments
avatar image abaza121 · Apr 23, 2018 at 02:43 PM 1
Share

Hi! you can use Transform.LookAt() and specify your pivot(up world) as hit.point, as for the range u can check Vector3.Distance(x,y) and check the distance between the player and the hit point (note that you might need to use the intersection of the player on the plane as x and the hit point as y)

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by tormentoarmagedoom · Apr 23, 2018 at 02:52 PM

Good day @strausse1

First, you must know you can instantiate something, and then change what you need. You should do something like this example:

          if (Input.GetKeyUp("q"))
          {
              GameObject ObjectToInstantiate= Instantiate(objectToInstantiate, hit.point, Quaternion.identity);
              nextFireTime = Time.time + cooldownTime;
              ObjectToInstantiate.transform.rotation = RotationYouWant;
              ObjectToInstantiate.GetComponent<Something>().somethingelse = false;
          }

As you see, you can declare a gameobject and then instantiate it, so you have a variable that can be modified as any other gameobject.

You need now to know how to set the correct rotation. Look at the comments, thare are good advises.


If helped, please accept the answer and close the question.

Or give more details of your problem!

Bye :D

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

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

80 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

Related Questions

Instantiate Prefab in another Prefab 0 Answers

Instantiate buttons , each with a unique positioning 2 Answers

How to instantiate random object in a panel ? 2 Answers

Instantiate 4 prefabs at 4 locations, shuffled 1 Answer

transform.Translate code isn't working 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