- Home /
Projectile move towards mouse cursor
I'm not looking for answers on how to make my character face my mouse cursor, but just so the bullet coming from the character will move towards where my mouse cursor was when it shot. The game is currently a 2d top down shooter and all my bullets do is essentially act like bombs. The instantiate, but do not move.
What code I have is here.
function BulletShot(){
var BulletClone = Instantiate(bullet, transform.position, Quaternion.identity);
rigidbody.AddForce(transform.forward * BulletSpeed);
Physics.IgnoreCollision(BulletClone.collider, collider);
}
Answer by Oliver Eberlei · Jun 16, 2011 at 10:57 PM
You can create a plane and do a raycast against it to get the current mouse position in 3d space (at y = 0)
This is C#, shouldn't be too hard to convert to JavaScript
if( Input.GetMouseDown( 0 ) )
{
Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
float distance;
if( zeroPlane.Raycast( ray, out distance ) )
{
Vector3 outputPosition = ray.origin + ray.direction * distance;
Debug.Log( "Position: " + outputPosition );
//You can use this position to rotate the bullet like so
BulletClone.transform.LookAt( outputPosition );
}
}
If you want the resulting position at a different height than zero, change the Vector3.zero in the first line.
I am having a couple of issues in changing this over to JavaScript.
if( zeroPlane.Raycast( ray, out distance ) ) { Vector3 outputPosition = ray.origin + ray.direction * distance;
That is confusing me quite a bit. First off is am assu$$anonymous$$g i am creating a variable called distance which is a float and not doing something such as Vector3.distance.
But then what is "out distance", I have been looking at the Unity scripting documentation and it isn't helping me out with understanding it. The actual Raycast has "out enter : float" anyway, but no matter what I put I still get errors.
And with the Vector3 outputPosition. Am I creating a variable Vector3 called outputPosition? Even if I am I haven't found a way to write it without just getting errors.
damn, deleted my comment -.- again
The out keyword is a C# construct (not sure if its the same in JS) which causes arguments to be passed by reference. This means the Raycast function can change the distance paramenter, and it will be changed outside the Raycast function as well.
Regarding the output position: Yes, this is a vector we create via the following calculation
ray.origin + ray.direction * distance;
(Note: ray.direction always has the length 1)
In simpler terms, by starting at
ray.origin (which is where we started our raycast)
and adding
"direction of ray" times "distance of hitpoint from origin"
we end up at the world position where the ray hit the plane.
Answer by aldonaletto · Jun 24, 2011 at 01:04 AM
If you're making a top down shooter with the camera set at some height and looking down to the ZX plane (rotation = 90,0,0), this script will shoot in the direction of the point the mouse is on. Don't forget to uncheck Use Gravity in the rigidbody:
var bullet:GameObject;
var BulletSpeed:float = 20;
function BulletShot(){
var myPos = transform.position;
var dist = Camera.main.transform.position.y-myPos.y;
var x = Input.mousePosition.x;
var y = Input.mousePosition.y;
var dir = Camera.main.ScreenToWorldPoint(Vector3(x, y, dist))-myPos;
var BulletClone = Instantiate(bullet, myPos, Quaternion.LookRotation(dir));
BulletClone.rigidbody.velocity = dir.normalized * BulletSpeed;
Physics.IgnoreCollision(BulletClone.collider, collider);
Destroy(BulletClone,5);
}
function Update(){
transform.Rotate(0,Input.GetAxis("Horizontal")*60*Time.deltaTime,0);
transform.Translate(0,0,Input.GetAxis("Vertical")*10*Time.deltaTime);
if (Input.GetButtonDown("Fire1")){
BulletShot();
}
}
It also included the movimentation instructions, but you can replace them with your own code.
Actually I am on the x,y plane. So I need the bullets to be able to move 360 degrees around my character while they don't actually do that with that code. If you are asking why I had transform.forward in my script, then don't ask it should have been that anyway.
Answer by Dreamblur · Jun 24, 2011 at 01:33 AM
The problem with your code is this line here:
rigidbody.AddForce(transform.forward * BulletSpeed);
rigidbody (unless it was explicitly overridden) is the access variable to the rigidbody of the game object to which your script is attached to. Since your bullet is being instantiated from within this script, then it's quite obvious that this script is not attached to your bullet -- and even if were, the function would have to be called again for rigidbody to start referring to the bullet's own rigidbody, in which case you'd have a second bullet with the same problem. You need to access the rigidbody of the bullet that you just instantiated, not the rigidbody of the bullet instancer.
So you basically want something like this:
BulletClone.rigidbody.AddForce(transform.forward * BulletSpeed);
But that doesn't solve all your problems.
You see, that line is inside the same function which instantiates the bullet, which means that it gets called after the bullet is instantiated and never again (not for the same bullet). And since you didn't pass a ForceMode parameter, then that will be set to the default value of ForceMode.Force. There's nothing wrong with that, but I'm thinking that's not what you want. Considering that ForceMode.Force was meant to be used so that force can be applied over several frames, I'm 100% sure that that's not what you want. What you actually want to do is apply an initial velocity to the bullet, which you do by passing either ForceMode.Impulse or ForceMode.VelocityChange as the ForceMode parameter.
As for the issue of wanting the character to face the cursor, the others have already addressed that. I haven't perused the actual code which they posted, but I'm too lazy to do so, so let's just say that I trust them. XD
Your answer
Follow this Question
Related Questions
Problem creating a 2D scroller shooting game 2 Answers
How to avoid speed change in bullets while moving? 1 Answer
2d shooting Left / Right 1 Answer
Fire a Projectile towards a specific object? 1 Answer
Shoot Bullet At Touch Position : 2D 1 Answer