- Home /
Answer by robertbu · Aug 13, 2013 at 09:47 PM
Saying aiming at the mouse position, you have to ask "at what distance." If your spawn point is not in alignment with your mouse cursor, then the calculation is a triangulation, and you need the distance in front of the mouse cursor. Start a new scene, put your spawn point back near the camera, and attach this script. Drag your prefab onto the 'prefab' variable in the Inspector. Left mouse click fires:
#pragma strict
var prefab : GameObject;
var distance = 10.0;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var position = Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
position = Camera.main.ScreenToWorldPoint(position);
var go = Instantiate(prefab, transform.position, Quaternion.identity) as GameObject;
go.transform.LookAt(position);
Debug.Log(position);
go.rigidbody.AddForce(go.transform.forward * 1000);
}
}
The above script, just set the distance to triangulate at 10. If you have specific targets in the scene, you will want to determine the distance to the target using raycasting. Also note that if you have gravity turned on in the Rigidbody attached to the projectile, it will pull the projectile down and the projectile will hit a bit low.
Thanks it works perfectly. I had to change distance to 50 though.
Answer by Narv · Aug 13, 2013 at 12:18 AM
You'll want to use Camera.ScreenToWorldPoint(Input.mousePoision)
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html
You'll get the point in the world space that the mouse is pointing at, then you can make your bullet along the path from the camera or spawn point, to that position.
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
spawn at gameobject position
Lerp FRO$$anonymous$$: prefabs current position, TO: Vector returned from ScreenToWorldPoint()
Last variable is time, try with Time.deltaTime and then play around with the value until you get the speed you want.
But that wouldn't work because if I move my character it calculates the updates differently and wont end up in the right spot. Also I don't think that would let me shoot multiple bullets.
I tried to rotate it to face the mouse point and then shoot forward, but for some reason it spawns and gets stuck.
var shootlaser : GameObject = Instantiate(laser, transform.position, transform.rotation);
var screenPos : Vector3 = camera.WorldToScreenPoint (target.position);
shootlaser.transform.LookAt(screenPos);
shootlaser.rigidbody.AddForce(shootlaser.transform.forward * force);
You are doing it backwards. I linked you ScreenToWorldPoint, not WorldToScreenPoint. You want to go towards the cursor correct? So you are basically "projecting" the mouse cursor from the screen, to where it hits the world.. so you would use Camera.ScreenToWorldPoint(Input.mousePosition) (as mentioned in my original answer)
var shootlaser : GameObject = Instantiate(laser, transform.position, transform.rotation);
var screenPos : Vector3 = camera.main.ScreenToWorldPoint(Input.mousePosition);
shootlaser.transform.LookAt(screenPos);
shootlaser.rigidbody.AddForce(shootlaser.transform.forward * force);
If you move your character, it should have no affect on the projectile as it's got a rigidbody force on it in a direction (the direction of the mouse projection to the world
This lerp thing is getting way to confusing for me. The last variable is anyways. I feel like the article threw in a bunch of info that wasn't needed. Also I want it to be smooth while flying and lerping works in frames or something. I feel like making that smooth would cause lag. Here is the line I used anyways:
shootlaser.transform.position = Vector3.Lerp(transform.position, screenPos, Time.deltaTime);
but honestly it's not worth it to use lerp.