- Home /
Raycasthit.point - same place every time! (Unity 3.5)
Hello. I'm trying implement a raycast shooting system for my FPS game. the problem is, everytime I try to instantiate a spark effect at the point of impact, it returns a location of (0,0,0) and creates the spark at that location no matter where I shoot it. Here is the relevent shooting code. Also, I am using Unity 3.5 (this may be the issue). Please note that the rocket contains no problems, only the Gun type. The object this is attached to is a child of the camera. Sickly code:
case weaponType.Gun :
var fwd = transform.TransformDirection (Vector3.forward);
var hit: RaycastHit;
if (Physics.Raycast(launchpoint.position,fwd,range))
Instantiate(explosionPrefab,hit.point,transform.rotation);
print("this could not be more actual");
print(hit.point);
}
}
/////////////////////////////////// full code:
private var curReload = 0;
enum weaponType {Rocket, Gun, Melee}
var reloadTime = 0;
var projectile: GameObject;
var explosionPrefab : Transform;
var currentWeapon: weaponType;
var Speed = 30;
var range = 10;
var launchpoint : Transform;
private var countReload = false;
function Start()
{
curReload = reloadTime;
}
function Update () {
print(curReload);
print(countReload);
if ((Input.GetButtonDown("Fire1")) && (curReload == reloadTime))
Fire(currentWeapon);
if (countReload)
{
curReload++;
}
else
{
return;
}
if (curReload == reloadTime)
{
countReload = false;
}
}
function Fire(currentWeapon : weaponType) {
animation.CrossFade("Shoot",0.2);
countReload = true;
curReload = 0;
switch (currentWeapon)
{
case weaponType.Rocket :
var projed =Instantiate(projectile,launchpoint.position,transform.rotation);
projed.GetComponent(Projectile).speed = Speed;
break;
case weaponType.Gun :
var fwd = transform.TransformDirection (Vector3.forward);
var hit: RaycastHit;
if (Physics.Raycast(launchpoint.position,-fwd,range))
Instantiate(explosionPrefab,hit.point,transform.rotation);
print("this could not be more actual");
print(hit.point);
}
}
Answer by save · Dec 29, 2011 at 07:34 AM
You never declare hit inside your Raycast, that should be the issue.
Sorry for the off topic, Unity 3.5 is released? (oh happy day!) :)
I think you're pretty much the best ever ;) It works! //////// for others having this problem, change it to something like var hit: RaycastHit; if (Physics.Raycast(transform,rotation,hit)));
I guess point is not the position raycast hits. just the gameobject poisition.
Your answer
Follow this Question
Related Questions
How to get a Vector3 from a RaycastHit variable? 1 Answer
Using raycast and collider to increase int 1 Answer
Raycast not working 2 Answers
Raycasting Through Terrain 1 Answer
Raycast exit point of collider 0 Answers