Now the prefab does not instantiate in the Players direction… I even tried:
if (Input.GetKey(KeyCode.LeftCtrl) {
GameObject instPrefab = (GameObject) Instantiate(prefab, transform.position, Quaternion.Euler(Input.mousePosition.y * 90, 0, 0));
instPrefab.rigidbody.velocity = instPrefab.transform.TransformDirection(-Vector3.forward * 100); // need to change the directional vector
}
and it is still not right! Basically, I have a mouse look script in which we can look up or down (mouse y) and we can strafe left and right using ‘a’ or ‘d’ and move forward and backward by using ‘w’ or ‘s’. I just want to angle the projectile in the direction the player is facing…
I even tried ScreenToWorldPoint but I find that to be a terrible result for what I want to acheive…
For my 3D game, The player should always instantiate a prefab with a certain velocity travelling in the way the player is facing, and also be able to angle the prefab (based on MouseY position)…I don’t see why that is so hard to acheive…
It shouldn’t be hard to achieve. I’m not completely sure I understand what behavior you’re wanting, but here’s one method you could try (untested):
// Get the player's orientation:
Quaternion rotation = transform.rotation;
// Rotate about the local x axis according to the mouse y position:
rotation *= Quaternion.Euler(Input.mousePosition.y * 90, 0, 0);
// Now use 'rotation' when instantiating the new object.
Using mousePosition.y directly to determine the angle seems a little suspect, but I’ll assume you have a good reason for doing it that way.
Thanks for the reply; Your code works excellently and I do appreciate your help. One thing that I would like to comment on from your reply:
“Using mousePosition.y directly to determine the angle seems a little suspect, but I’ll assume you have a good reason for doing it that way.”
This is just what I presumed to be the correct way of angleing a projectile in both FPS and TPS (and also what feels most comfortable to navigate around with).