- Home /
Instantiate in front of player (and give velocity)
Hi,
For some reason, I cannot instantiate in front of the player after I rotate him. How can I keep the players y rotation the same as when I instantiate a projectile?
the script that I am doing this in is attached to the player:
GameObject instPrefab = (GameObject) Instantiate(Prefab, transform.position, Quaternion.Euler(Input.mousePosition.y * factor, Input.mousePosition.x, 0));
instPrefab.rigidbody.velocity = instPrefab.transform.TransformDirection(-Vector3.forward * 200); // is not fully in front of the player
Answer by Jesse Anders · Dec 08, 2010 at 06:02 AM
I didn't try to figure out everything that's going on there, but it looks like you're making the common mistake of trying to treat the elements of a quaternion as (Euler) angles. This, for example:
Quaternion(transform.rotation.x, transform.rotation.y, transform.rotation.z, 1)
Almost certainly doesn't do what you think it does, and is very unlikely to do anything useful.
In short, use Transform.rotation and .localRotation when you want to work with the orientation directly in quaternion form, and Transform.eulerAngles and .localEulerAngles when you want to work with the orientation in Euler-angle form.
It seems that when I need to fire my projectile at an angle, I can't get the instantiation to always appear in front of the player.
Look at Transform.TransformPoint() (it will allow you to specify a point that's relative to the player's current position and orientation).
I can't really tell from your code what you're trying to do. (For one thing, to instantiate something in front of the player, you would typically offset the position relative to the player's position, but you appear to be using the player's position unmodified.)