- Home /
Shoot at Mouse Cursor
Could someone please help me? I have this code and im not sure why it wont work.
public float BulletSpeed;
public float Damage;
// Use this for initialization
void Start () {
var playerPlane = new Plane(Vector3.back, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0f;
if (playerPlane.Raycast (ray,out hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
targetRotation.x = 0.0f;
targetRotation.y = 0.0f;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 500 * Time.deltaTime);
}
}
// Update is called once per frame
void Update () {
transform.localPosition += (transform.TransformDirection(Vector3.left * BulletSpeed));
}
I believe it takes the rotation of the bullet and changes to where the mouse cursor is at then it shoots, but it can only shoot on the left side correctly (Probably due to the Vector.left).
I want it to shoot straight when its rotated correctly, Like when the bullet rotation is 90 degrees it will shoot straight at 90 degrees according to where the mouse cursor is at.
Answer by Berenger · Jun 20, 2012 at 05:28 PM
You shouldn't manipulate a quaternions value like that (.x = 0). Try the Euler function if you want to set axes to 0.
Your answer
Follow this Question
Related Questions
Shoot at mouse cursor 0 Answers
Attaching position of an object in game to position of cursor on screen 0 Answers
Help in GUI issue - Can't lose focus of a dropdown list when mouse clicks somewhere else? 0 Answers
How to shoot a bullet to curser position? 2 Answers
How do I make my mouse go back to default after clicking empty space? 0 Answers