- Home /
shoot to the mouse cursos
Hey guys im trying to do a 2d game shooter ( not spirit, 3d aspect ) , and im trying to change a shooting script where instead of shooting forward , i shoot toward the mouse cursos but i get errors ,here what i tried
function shoot () {
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward); // <== (the part i want to change)
Debug.DrawRay (transform.position ,DirectionRay * Range , Color.blue);
if (Physics.Raycast(transform.position , DirectionRay , Hit, Range)){
if (Hit.rigidbody){
Hit.rigidbody.AddForceAtPosition ( DirectionRay * Force , Hit.point);
Hit.collider.SendMessageUpwards("ApplyDamage" , Damage, SendMessageOptions.DontRequireReceiver);
}
}
i tried with:
var Directionay = Camera.main.ScreenPointToRay (Input.mousePosition );
but didn't work lots of methode errors =(
Take a look at this, at 17:40 , he makes an arm rotation script , i guess this is what you are looking for.You can use that rotation value for your shooting script.
And there is a small mistake , line 8: the paranthesis should close just before the semicolon ");" and not just include the input mouse position.
the problem is that i'm using 3d object and unfortunatly it work better in spirit =( bad luck
Answer by toddisarockstar · Feb 20, 2015 at 06:49 AM
put it back how it was. the line you took out works fine. your getting an error cause your feeding raycast to much info. saying Camera.main.ScreenPointToRay (Input.mousePosition ) gives the raycast all the info it needs.
just say:
if (Physics.Raycast(DirectionRay , Hit, Range)){}
without the "transform.position" part and you wont get errors. raycast format is only one point and one direction. so the first ray is a line from camera THROUGH mouse.
if you want a direction for your bullet, your variable "Hit" contains the location of the mouse. you can pull a vector three out of it by saying hit.point.
the "hit" varaible is what the mouse clicked on. not your bullet.
well i'm on a good spot but i can't manage to fix the problems in the rigidbody part i get : "BCE0051: Operator '*' cannot be used with a left hand side of type 'UnityEngine.Ray' and a right hand side of type 'float'." it's in the line 13 (almost theire!!)
yeah, it wants a direction in vector three format. not raycast. its a different vaiable type. vector three can also represent direction if you simply subtract one vector three location from another. so we want to represent the objects position to where the mouse clicked. right? the raycast was from the camera down and i dont think you want things going down anyways.
so we i think this is the direction you want:
var blastdirection:Vector3;
blastdirection=Hit.point-tranform.position;
rigidbody.AddForce (blastdirection * 10);
wait does this meen that i have to take down the function raycast part and change all?
no, the rayscast part is good to find mouse position. but then you need to get direction from the character/bullet to the mouse click spot to represent wich way the "hit" object goes. I didnt test it but all together i think you would say it something like this:
function shoot () {
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward); // <== (the part i want to change)
Debug.DrawRay (transform.position ,DirectionRay * Range , Color.blue);
if (Physics.Raycast(transform.position , DirectionRay , Hit, Range)){
if (Hit.rigidbody){ var blastdirection:Vector3;
blastdirection=Hit.point-tranform.position;
rigidbody.AddForce (blastdirection * 10);
}
}
at line 13 i get unknown identifier : "transform"(point-tranform.position; is a little odd never seen it).
Answer by seb-lopez · Feb 22, 2015 at 02:37 PM
hey i was thinking of a new way to make my dream shoot a reality. should i let the raycast script as before( streght line) and instead make the spawn point (empty game Object ) rotate towards the mouse would it be more easy or the post way is more easyer?
if it where my script i would simple move the object to the mousepoint.
var hit:RaycastHit;
var guythatgotshot:GameObject;//<--im sure you'll need it lol.
var guythatisshooting:GameObject;//<--this is where bullet starts!
var gobullet=false;
var target:Vector3;
guythatisshooting=GameObject.Find("name of shooter guy");
function Update(){
if(gobullet){
transform.position = Vector3.$$anonymous$$oveTowards(transform.position,target, 1);}
if(Input.Get$$anonymous$$ouseButtonDown(0)){
transform.position=guythatisshooting.transform.position;
var Directionay = Camera.main.ScreenPointToRay (Input.mousePosition );
if (Physics.Raycast(DirectionRay , hit)){gobullet=true;
guythatgotshot = hit.collider.gameObject;
target=hit.point; } } }
honestly i dont think i would bother with addforce or anything like that unless you needed the object to bounce or something. this is a full script for what i think your talking about. make a new script with just this and stick it to your bullet object. i didnt test it but it should work. you shouldnt need to "spawn" a bullet. just put it in your scene with this script and it will just keep recycling itself.
i will try ( sorry for late respond school homework came up)