- Home /
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..
I need to give the projectile an angle upon instantiation. So it makes sense to use the Quaternion.Euler in the Instantiate method..
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));
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
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.
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..
Your problem is your velocity. You set velocity to be the transform's up, not the instance's up.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090055im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shoot at an angle 2D 2 Answers
Randomly instantiate bullets within a certain angle based on look direction 1 Answer
Instantiate Angle? 1 Answer
Checking if object intersects? 1 Answer