Apologies for the long question I wasn't sure what else to title it.
I currently have a cube that is fired and when it collides with something it instantiates a cylinder (large flat cylinder, looks like a decal but its an object, which is what we need just comparing it to a decal in visual aspects, I do not want to use decals!) What I am trying to do is have the cylinder's up/y axis to allign to the surface of the object it has hit. For example if it hits the floor it should be flat against the floor and if it hits a wall it should be flat against the wall. At the moment it hits a wall but instantiates as though it has hit floor. So I looked into sphere casting and cannot implement it properly, I've looked at and deconstructed several scripts to get to where I am now as well as following useless tutorials which tell me to do one thing instead of explaining the theory behind using spherecast.
var hit : RaycastHit;
function Start()
{
//THis is the line that gives me errors, not sure what to put in the brackets
//i'v tried so many things Iv lost the plot of what i was trying to do with this
if (Physics.SphereCast (hit.transform))
{
var ImpactPoint = hit.transform.rotation;
var currentPos = transform.rotation;
var newpos = Quaternion.Lerp (currentPos, ImpactPoint,1);
transform.rotation = newpos;
}
}
What I've managed to achieve doesn't run as I keep hitting errors due to my lack of understanding, but I'm hoping it will work and if it wont then should I just start looking for another method to achieve this? (like messing around with tagged colliders, don't really want to do it but it's always a last resort)
Edit
apologies for not being here for a couple of days, started at a party, end up in Cambodia enough said. Anyway I've had a look at all your helpful answers (many thanks to you all), had a good read of your explanations and had a look at the script reference to try understand it all. I have tried implementing the answers I have recieved here and came across a common error, when my object (projectile that spawns the prefab SugarAOE) hits the floor and spawns the projectile it is always facing forward in stead of facing up/down. Likewise when it hits a wall it spawns facing up/down instead of facing sideways/forward. Loving the fact that it's working better than my (admittedly shody) attempt at spherecasting, feels like I'm close. Will work on it constantly till I crack it but if any more generous people would like to explain what I'm doing wrong I'd be thrilled to listen.
As per someones request here is the script that instantiates the prefab that I'm trying to spawn flat on a surface. This is on a projectile which is instantiated via another script. I've removed the other attempt (as above) from it's script.
var SugarAOE : GameObject;
function OnCollisionEnter(collision : Collision)
{
SpawnPrefab();
Destroy(gameObject);
//audio.clip = bang;
//audio.Play();
}
function SpawnPrefab()
{
var origin : Vector3 = transform.position;
var direction : Vector3 = transform.up;
var hit : RaycastHit;
var distance : float = Mathf.Infinity;
if (Physics.Raycast(origin, direction, hit, distance))
{
Instantiate(SugarAOE, hit.point, Quaternion.LookRotation(hit.normal));
}
}
As you can see I decided against the spherecast as mainly it was $h|? and didn't work at all, very unexpected things kept on happening, when it hit something and spawned it would pick a random place to do it, eg if i fired at exactly 0,0,0 in the scene and it hit my floor (which is around 0,0,0) then some how it would end up in a completely random place. Maybe that's me being useless or setting it up wrong I've no idea? I still don't fully understand it even though I'm trying hard, going to look at some more documentation and cross reference it with your answers to see where I am going wrong.
So in this edit's conclusion I either need to figure out why the sphereCast acts so strangely or I need to figure out when using raycast how to alter the direction in which it is spawned?
EDIT #2
Haha I've figured out why it acts so strangely! I have a random rotation added to my projectile! (It's a rigidbody so I mean it has AddTorque) I believe this is why i need the sphere cast limited to a small radius. Only noticed when every now and then it doesn't find the right object and instead does an infinite raycast to the edge of my level (didn't even know there was a collider on it? Lucky there is) obviously if I adjust the raycast length to be less every now and then I get nothing (which is expected).
EDIT #3
Having major problems getting the sphere cast to work properly, it appears completely random as to what it decides to align itself to and it's orientation. I changed the title of the question, now what I'm having trouble with, like I said, is ShpereCasting properly from a rotating object. I can't wrap my head around it, I don't get consistent or monitorable results so I've no clue how to go about fixing it. My brain hurts :(
This is what I have so far:- I instantiate a randomly spinning (all rotational axis) projectile which has this script on it
var SugarAOE : GameObject;
var PosCast : Vector3;
var RotCast : Vector3;
function Update()
{
var origin : Vector3 = transform.position;
var radius : float = 2;
var direction : Vector3 = transform.forward;
var hit : RaycastHit;
var distance : float = 2;
if (Physics.SphereCast(origin, radius, direction, hit, distance))
{
PosCast = hit.point;
RotCast = hit.normal;
}
}
function OnCollisionEnter(collision : Collision)
{
SpawnPrefab();
Destroy(gameObject);
//audio.clip = bang;
//audio.Play();
}
function SpawnPrefab()
{
scaoe = Instantiate (SugarAOE);
scaoe.transform.position = PosCast;
scaoe.transform.up = RotCast;
}
The problem now is it pretty much never targets the floor. Seriously What should I do? any direction that may yeild a positive result. Other things I've tried include :-
function SpawnPrefab()
{
var origin : Vector3 = transform.position;
var direction1 : Vector3 = transform.up;
var direction2 : Vector3 = transform.forward;
var direction3 : Vector3 = transform.right;
var hit : RaycastHit;
var distance : float = Mathf.Infinity;
if (Physics.Raycast(origin, direction1, hit, distance))
{
SAOE = Instantiate(SugarAOE);
SAOE.transform.position = hit.point;
SAOE.transform.up = hit.normal;
}
if (Physics.Raycast(origin, direction2, hit, distance))
{
SAOE = Instantiate(SugarAOE);
SAOE.transform.position = hit.point;
SAOE.transform.up = hit.normal;
}
if (Physics.Raycast(origin, direction3, hit, distance))
{
SAOE = Instantiate(SugarAOE);
SAOE.transform.position = hit.point;
SAOE.transform.up = hit.normal;
}
}
FINAL EDIT
Huzzah I've done it, it's probably a bit messy and I will keep this question open to see if anyone has a better way?
var SugarAOE : GameObject;
var HasSpawned : boolean = false;
private var where : Vector3;
private var whichway : Vector3;
function OnCollisionEnter(collision : Collision)
{
SpawnPrefab();
Destroy(gameObject);
}
function SpawnPrefab()
{
var origin : Vector3 = transform.position;
var direction1 : Vector3 = transform.up;
var direction2 : Vector3 = transform.forward;
var direction3 : Vector3 = transform.right;
var hit : RaycastHit;
var distance : float = 1;
if (Physics.Raycast(origin, direction1, hit, distance))
{
if (HasSpawned == false)
{
where = hit.point;
whichway = hit.normal;
AreaOfEffect();
}
//SAOE = Instantiate(SugarAOE);
//SAOE.transform.position = hit.point;
//SAOE.transform.up = hit.normal;
}
if (Physics.Raycast(origin, direction2, hit, distance))
{
if (HasSpawned == false)
{
where = hit.point;
whichway = hit.normal;
AreaOfEffect();
}
}
if (Physics.Raycast(origin, direction3, hit, distance))
{
if (HasSpawned == false)
{
where = hit.point;
whichway = hit.normal;
AreaOfEffect();
}
}
}
function AreaOfEffect()
{
HasSpawned = true;
SAOE = Instantiate(SugarAOE);
SAOE.transform.position = where;
SAOE.transform.up = whichway;
}
So in answer to my question "How to ShereCast consistently from a rotating object?" Don't would be the answer. Much as Statment said very early on use raycast instead. The whole rotating object thing makes it quite difficult.


If you are using small radius (< 5? absolutely not as big as 10) you might get away with it as errors were generally noticed with bigger spherecasts.
– Statementyes I wish to use a tiny radius it doesn't need to be big at all. Just big enough to encompass projectile object so that when the projectile collides with something the spherecast parameters are in effect.
– anon61858128