hi, im been trying to do this raycasting thing to help the camera aways focus on the character, and if the character meets a wall, i dont want the camera to back away and show the other side of the poligons of that wall… so basicly what i need to do is to check if theres nothing crossing the line bonding the camera and the character
i made a script and attached to the camera, here it is
var correct_number : // actualy, this should be the right ammount of distance the ray has to be from the character… so the “if statement” will check, if the distance cuts short, then something got on the way, so the camera will aproach the character
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.forward, hit)) {
var raywidth = hit.distance;
}
if ( raywidth < correct_number )
// do camera aproaching
}
but!.. i does not work…
couple of things here.
-
Your if (raywidth < correct_number) doesn’t have an opening curly brace.
-
Your raywidth is declared inside the scope of the “if” block for your Physics.Raycast, but you use it outside that “if” block.
Perhaps simplest solution is to move your “do camera approaching” block within the raycast call:
function Update ()
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.forward, hit))
{
var raywidth = hit.distance;
if ( raywidth < correct_number )
{
// do camera aproaching
}
}
}
Okay!.. it … kinda works… but only in certain moments…
I’m sorry Warp boy2, I don’t follow. Can you please try rephrasing?
sorry about that i wrote exacly what i was thinking without translation xD
my question was…
when i cast a ray in the ( vector3. transform.forward) direction … how do i know that… this forward, is aimed at the my character?
since the camera have a shperical shape… the direction forward could be anywhere…
OR , wait…
since the camera is using the “LookAt” command into the character, it means that , the “forward” direction is facing the character?
Rays will shoot in the direction you want.
So if you have your forward direction (commonly z) of your object face your target to start with, then shoot a ray in that direction, you can test for objects in the way…
This is an example of what I am doing currently - I get a tank to drive around the scene, when it detects my player in the vicinity, it adjusts its hatch + turret to face my player, then I detect if something is in the way (a building), if so, dont fire. It only fires if there is a clear line of site.
My hatch turns on the Y, the little turret sticking out is a child and only needs adjusting on the ‘height’ (so its x, so it imitates a tilt up towards the chopper), once thats done, I check for absolutely ANYTHING being in the way before firing.
Remember your forward axis!
#region attack chopper
if (isAttacking)
{
// the hatch
Quaternion rot = Quaternion.LookRotation(Player.position-TurretHatch.transform.position);
TurretHatch.transform.localRotation = Quaternion.Lerp(TurretHatch.transform.localRotation, rot, Time.deltaTime*attackSpeed);
// the turret!
rot = Turret.transform.localRotation;
Turret.transform.localRotation = Quaternion.Lerp(rot, Quaternion.Euler(maxTurretAngle,rot.eulerAngles.y,rot.eulerAngles.z), Time.deltaTime*attackSpeed);
// check if something is in the way, otherwise - fire!
Vector3 fwd = Turret.transform.TransformDirection(Vector3.forward)
if (!Physics.Raycast(Turret.transform.position, fwd, attackDistance))
{
print("nothing in the way!");
BroadcastMessage("Fire");
}
else print("Get out of the way!");
}
#endregion
Hope this helps
Hellaeon!
Thank you both!, thanks hellaeon
i got it to working now… your informations and lots of trial and error made it possible… thanks!