• 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 lampshade · Dec 07, 2010 at 05:44 AM · instantiateangle

Height of Mouse to Determine the Angle

Hi,

Currently, I am instantiating a projectile at a desired position in 3DSpace via the following line of code:

GameObject instPrefab = (GameObject)Instantiate(Prefab, new Vector3(transform.position.x, transform.position.y+20, transform.position.z), Quaternion.identity);

/instPrefab.transform.rotation = Quaternion.Euler(transform.rotation.y+(Input.mousePosition.y*factor), transform.rotation.x,transform.rotation.z); // no angle instPrefab.rigidbody.velocity = transform.TransformDirection(Vector3.up 200); // is instantiated in front of the player*/

instPrefab.rigidbody.velocity = CurrentSpell.transform.TransformDirection( -Vector3.forward*200); // gives an angle instPrefab.rigidbody.velocity = transform.TransformDirection(Vector3.up * 200); // not instantiated in front of player

It seems like I can get the angle, but the projectile isnt in front of the playerm and when I can't get the angle, the projectile is in front of the player.

I thought I could get it angled by modifiying the prefabs rotation (after instantiation) like this:

But I am not seeing any angle for when I move the mouse up and down..

Comment
Add comment · Show 1
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 lampshade · Dec 07, 2010 at 07:03 PM 0
Share

I need to give the projectile an angle upon instantiation. So it makes sense to use the Quaternion.Euler in the Instantiate method..

1 Reply

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

Answer by skovacs1 · Dec 07, 2010 at 03:59 PM

You would use the rotation Quaternion of the Instantiate. Probably something like:

public float rotationRange = 90.0f; //degrees float factor = 1.0f;

public void Start() { factor = rotationRange / Screen.height; }

//Somewhere else Instantiate(prefab, position, Quaternion.Euler(Input.mousePosition.y * factor,0.0f,0.0f));

Or if you want something more directly related, you could convert the mouse position into a world coordinate and then point at it.

//depth is how far from the camera you want the aimPosition to be
Vector3 aimPosition = new Vector3(0.0f, Input.mousePosition.y, depth);
aimPosition = Camera.main.ScreenToWorldPoint(aimPosition);
Instantiate(prefab, position, Quaternion.LookRotation(aimPosition));
Comment
Add comment · Show 9 · 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 lampshade · Dec 07, 2010 at 06:02 PM 0
Share

Your first code snippet is what I was looking for, but does not work. Your second code snippet is what I have already tried (sorry for not specifying that) and is not a desired result. I reposted with something similar

avatar image skovacs1 · Dec 07, 2010 at 07:04 PM 0
Share

What exactly about the first snippet does not work? I tested it, passing in a prefab and position variable, calling the instantiate on mousebuttondown inside an Update and it worked just fine.

avatar image lampshade · Dec 07, 2010 at 07:19 PM 0
Share

I am trying Input.mousePosition.y for all x,y and z paramaters in Quaternion.Euler....my projectile's velocity has no change in its angle..

avatar image skovacs1 · Dec 07, 2010 at 07:23 PM 0
Share

Your problem is your velocity. You set velocity to be the transform's up, not the instance's up.

avatar image skovacs1 · Dec 07, 2010 at 09:21 PM 0
Share

Don't use 'transform.TransformDirection(Vector3.up*100)' because that refers to a transform that you never rotated. Use 'velocity = instPrefab.transform.TransformDirection(Vector3.up*100)' or to save yourself the typing, simply 'velocity = instPrefab.transform.up*100' to use the transform that you actually did rotate.

Show more comments

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

No one has followed this question yet.

Related Questions

Checking if object intersects? 1 Answer

Instantiate objects in a line between two points? 2 Answers

Instantiate Object in direction from another 1 Answer

Instantiate Angle from a Child Object 1 Answer

How do i get a attack offset of the player at a fixed distance in 2D? 1 Answer


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