- Home /
Sidescroller Bullets Follow Mouse After Being Shot
So after I instantiate my bullets and fire towards my mouse in this sidescroller, the bullets continue to follow the mouse. If you move the mouse left, the bullets will curve left. The speed of the bullets will also be slower if you shoot close to the player, because the bullet wants to stay around the mouse. Here's a short video so you can visualize my problem: http://youtu.be/tT09CA-pY9M .
Now the code on my PLayerScript (To find location of the mouse and instantiate the bullet)
// this creates a horizontal plane passing through this object's center
var plane = Plane(transform.position, Vector3.up);
// create a ray from the mousePosition
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// plane.Raycast returns the distance from the ray start to the hit point
var distance: float;
if (plane.Raycast(ray, distance)){
// some point of the plane was hit - get its coordinates
hitPoint = ray.GetPoint(distance);
hitPoint.z = -19;
}
direction = hitPoint - transform.position;
if(Input.GetMouseButtonUp (0) )
{
var instantiatedbullet = Instantiate(bullet,GameObject.Find("SpawnPoint").transform.position, Quaternion.Euler(direction.normalized));
}
Then my code on my BulletScript to move it forwards after being instantiated.
//amount to move bullet
amtToMove = bulletSpeed * Time.deltaTime ;
transform.Translate(Vector3.fwd * amtToMove);
So how do I have the bullet remember the spot the cursor was at when the mouse button was clicked up, and only move to and then past that point?
Try something like this,
function Update(){ mouseX = Input.GetAxis("$$anonymous$$ouseX")l if (mouseX){ bulletTransform.Rotate(mouseX Time.deltaTime sensitivityVariable, 0, 0); } }
Hey mate, I posted an incomplete answer earlier but after thinking on it a bit more, your problem is:
var instantiatedbullet = Instantiate(... Quaternion.Euler(direction.normalized));
You're tying the bullet's rotation to a global variable that gets updated with each frame, so it's rotation will be wherever the mouse is pointing at the time and it'll transform.Translate(Vector3.fwd * amtTo$$anonymous$$ove) towards that rotation.
since hitPoint isn't prefixed by a var in your script I still think it's global, try instantiating it within the function you're using to make it local and all should be fine.
Can't wait the hour it takes for the actual answer to finally be visible so that I can edit it.
I use hitPoint as a global variable so my spawn point will look at it while rotating around an empty game object. $$anonymous$$y firend who programs in C# but not in unity quickly wrote up a code that would help me find the angle between the player and the mouse and just shoot along that angle. The code is this (part Pseudo code).
mPos = vector3 (mouse position) pPos = vector3 (player position) bPos = vector3 (Bullet position)
xdir = mPos.x - pPos.x ydir = mPos.y - pPos.y
angle = Atan2(xdir,ydir)
lx = cos(angle) ly = sin(angle)
bPos.x += bullet$$anonymous$$oveSpeed lx Time.deltaTime bPos.y += bullet$$anonymous$$oveSpeed ly Time.deltaTime
So my question now is, would that work properly on a 2d sidescroller, and how would it look if it was converted to usuable javascript? Thanks for any help, and thank you for your previous help as well.
Answer by Gennadios · May 15, 2012 at 02:21 AM
I can't help overhaul your code because I'm not particularly with Javascript, but is the
if (plane.Raycast(ray, distance)){
// some point of the plane was hit - get its coordinates
hitPoint = ray.GetPoint(distance);
hitPoint.z = -19;
}
block in the Update function, and is hitPoint global? If both are true it looks like a new hitPoint is generated every update based on wherever your mouse is at the time. Trying firing multiple bullets at once and see if they all follow the mouse.
Try passing a copy of the hitPoint to the bullet of using a global.
Answer by Pandiux · May 15, 2012 at 02:20 AM
Try something like this,
function Update(){ mouseX = Input.GetAxis("MouseX")l if (mouseX){ bulletTransform.Rotate(mouseX Time.deltaTime sensitivityVariable, 0, 0); } }