- Home /
[SOLVED] Shooting fail (Due to camera setup)
Hi, so I've set my camera to respond to both the WASD and mousemovement, for everyone that's played Counterstrike, it is of a similar function, if not, then here's a brief explanation W: Forward A: Strafe left S: Back D: Strafe right.
Mouse moves right = Turns right Mouse moves left = Turns left (Both concept similar to steering).
I'll paste the code for the camera if needed.
Anyways - The real question is when I shoot a clone from the spawnpoint (the spawnpoint is set within the elemental staff) it shoots with the WASD, but when I turn with the mouse, it shoots from it's last know location with the WASD. So how do I make my camera & fire understand that when I turn with the mouse, you shoot at what he's looking at rather than shooting backwards :/
var projectile : Transform;
var bulletSpeed : float = 20;
function Update () {
// Put this in your update function
if (Input.GetMouseButtonDown(0)) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
clone = Instantiate(projectile,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * 1000);
}
}
UPDATE -
I changed the following line
clone = Instantiate(projectile,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
into
clone = Instantiate(projectile,GameObject.Find("spawnPoint").transform.position, transform.rotation);
Now that allows me to shoot when the mouse movement changes, but upon shooting it doesn't shoot straight anymore!
Anyone got any ideas?
[SOLVED] - I adjusted my spawnPoint, apparently it was upside down.. Rofl. Blah.
Answer by ExTheSea · Apr 09, 2012 at 12:17 PM
You have to instantiate the clone also at the transform.rotation your looking at. So i think you should ad GameObject.Find("spawnPoint").transform.rotation after the position when spawnpoints points in the direction you want the clone to move in.
Answer by gregzo · Apr 09, 2012 at 12:07 PM
Adapted from the docs, TransformDirection :
// Calculate the x-axis relative to the camera
var cam : Transform = Camera.main.transform;
var cameraRelativeForward : Vector3 = cam.TransformDirection (Vector3.forward);
// Apply a force relative to the camera's x-axis
rigidbody.AddForce (cameraRelativeForward * 1000);
Even applying this technique or trying to make it work around my code. I still get errors, I see what it's trying to do, but in order to use this, I'd have to remove all my code :/
Your answer
Follow this Question
Related Questions
Shooting length 1 Answer
Enemy not getting hit (Collider issue) 2 Answers
[SOLVED] Shooting from Elemental Staff 1 Answer
[Solved] Generating Particles with Attack Animation 1 Answer