I instantiate missiles and make them look at the world point designed by the mouse.
It work almost perfectly, except for the lower part of the screen, the missile take stange trajectories.
Here is my “aiming cross” code:
static var viseurPositionX;
static var viseurPositionY;
static var hit : RaycastHit;
static var hitted;
static var hitTag;
function Update () {
Screen.showCursor = false;
//aiming cross position
viseurPositionX=Input.mousePosition.x/Screen.width;
viseurPositionY=Input.mousePosition.y/Screen.height;
transform.position.x=viseurPositionX;
transform.position.y=viseurPositionY;
//raycasting to see what we hit.
hitted=0;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, Mathf.Infinity))
{
hitTag=hit.transform.tag;
}
}
and the code which “launch” the missiles:
//transforms
var weapon02 : Transform;
var missilePrefab : Transform;
//import from others scripts
var viseurInfos : ViseurInfos;
//others
var missile = new Array ();
private var missileCount=0;
function Update () {
//targeting
var hit=viseurInfos.hit;
var hitTag=viseurInfos.hitTag;
//and... FIRE! (Missiles)
if(Input.GetButtonDown("Fire2"))
{
missile[missileCount]=Instantiate(missilePrefab, Vector3(transform.position.x,transform.position.y-1,transform.position.z), transform.rotation);
missile[missileCount].LookAt(hit.point,Vector3.up);
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = hit.point;
missileCount++;
print(missileCount);
}
}
As you can see I instantiate a Sphere at the hit point, and it works, but the missile take a strange trajectory if the mouse cursor is in the lower part of the screen.
How are the missile flying? Do they have some code on them to move them forward? Or are you just instantiating them to see how they react to the mouse?
And what do you mean by strange trajectories? Are they missing the target? flying up? down? in reverse?
Do the spheres appear correctly in the correct positions?
What do you have in your scene? Remember - raycasting without some king of layer mask can cause your ray to hit some collider that you didn’t think of, like maybe the collider or your own character, which is in the lower part of the screen maybe?
The only trouble with using “LookAt” is if you tell it to look in the direction LookAt is using as it’s Up vector, as that leads to a singularity in the rotation it creates.
This is only a problem if you are aim in the direction of (0,1,0) by default.;
Let’s see if I understand, the default up vector is Y I guess?
So if my aim is in that direction it can lead to what I’m experimenting?
Because in my system the aim is movable by de mouse, so it will, at some point, inevitably look at the default Up vector… as well as the other vectors :?
No, I don’t set it up myself.
So it’s kind of a dead end for the lookat for my purpose I suppose?
The other solution I’m thinking is to cast a ray from the missile and rotate it until the ray hit the target, but I don’t know how to calculate in wich direction the missile need to rotate :lol:
First of all - try and move the line of code that has the LookAt outside of the Input.GetButtonDown(“Fire2”) block. Fire one missile and move your mouse while it’s flying.
If it’s outside of the block (but still in the Update function) it will update the LookAt every frame, and you’ll be able to move the mouse slowly and see exactly where the LookAt fails.
Kind of hard to figure out what exactly is going wrong there without access to the scene itself, so
if you hit a dead end there, simply avoid doing LookAt. When you create the missile, calculate it’s rotation yourself.
Something along the lines of:
if(Input.GetButtonDown("Fire2"))
{
var v3InstantiationPoint = Vector3(transform.position.x,transform.position.y-1,transform.position.z);
var v3RelativePos: Vector3 = hit.point - v3InstantiationPoint;
var qMissileRotation = Quaternion.LookRotation(v3RelativePos);
missile[missileCount]=Instantiate(missilePrefab, v3InstantiationPoint, qMissileRotation);
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = hit.point;
missileCount++;
print(missileCount);
}
Oh, and A quick question - that yellow trail is the trajectory the missile is supposed to take? If so - why isn’t it following it even if the hit is correct?Is the camera moving or something like that?
And in the image where you are aiming down - where is the sphere that was supposed to be created when you hit the fire button?..
I tried that and the lookat fails… nowhere :shock:
The missile is following the hit point defined by the mouse position until he hit something and explode. Whatever the mouse position is. No strange trajectorie here. Maybe a hint to know where the problem is?
I’ve tried your code, Did exactly the same thing as mine. I begin to think that the lookat is not the problem…
No that’s the trajectory the missile took, I just increased the life length of the particles trail to have a graphical representation of the problem
I don’t understand exactly your question, but yes the camera is moving.
Nowhere, because I deactivated the sphere instantiation for the screenshots
I put the hit point in an array cell, and outside of the Input.GetButtonDown(“Fire2”) I put a loop who test all missiles and make them look at their assigned targets.
Quick&Dirty but… it work. If you find another solution, just tell me