- Home /
Spherecast if hit.distance <=, move the otherway
I'm writing a simple(!) script, where player is chased by an enemy. This enemy should never actually touch the player, and like a school playground game, if the player turns around or is about to collide with enemy, the enemy would run away.
However, it's not quite working, I keep getting a warning in regards to the following line:
if(hit.distance <= 5.0){
This is my entire update function:
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
var layerMask = 8;
var hit : RaycastHit;
var charCtrl : CharacterController = GetComponent(CharacterController);
var p1 : Vector3 = transform.position + charCtrl.center;
if (Physics.SphereCast (p1, charCtrl.height / 2, transform.forward, hit, layerMask, 10.0)) {
distanceToObstacle = hit.distance;
if(hit.distance <= 5.0){
transform.Rotate(0,25,0);
}
}
}
This my first experiment with SphereCast, so that could be the problem!
Thank you in advance!
"The referenced script on this Behaviour is missing!" Doesn't make sense =/
Answer by aldonaletto · Jul 03, 2012 at 12:40 PM
The last two arguments for SphereCast are in the wrong order: they should be range and layerMask. Check also if the layer mask is ok, or remove this parameter - Unity will assume all layers by default.
thanks Aldo, didn't realise that. Still not perfect though. Seems to ignore all collisions now. Perhaps layermask isn't right, will have to double check but it was fine.
Yes, this way you use layer "3", not layer "8" ( == 2^3. The Layermask is a bitmask, not a simple number that specifies a single layer. Each bit in the mask represents one layer.
Binary wise your "8" looks like this:
00000000 00000000 00000000 00001000
|||||
||||\- layer 0
|||\-- layer 1
||\--- layer 2
|\---- layer 3
\----- ...
If you want the layer "8" you have to use either 2^8 == 256 or use the Layer$$anonymous$$ask type which can convert this for you.
I can't imagine any reasonable warning on this line:
if(hit.distance <= 5.0){
It would help to know what the warning is telling you. Are you sure it's on this line?
I figured out that, the warning was unrelated and due to a prefab missing a script. odd it kept redirecting me to that line though.
However, i almost certainly haven't used layermasking properly, so am going to have to learn about it!
var layer$$anonymous$$ask :Layer$$anonymous$$ask = 1<<8;
seems to do the trick
Oh, and this is where I really am stuck, how can I take the enemy position and then basically reverse it if the enemy is too close?
i.e add a force in the opposite direction
How are you moving the enemy? With the code in the first two lines of Update? If so, revert the speed with something like this:
private var dir: float = 1;
function Update () { var moveDir = target.position - myTransform.position; var dist = moveDir.magnitude; if (dist 10) dir = 1; // restore direction if > 10 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(moveDir), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward dir moveSpeed * Time.deltaTime; ... } $$anonymous$$aybe you don't even need the SphereCast (unless you want to use it to avoid other obstacles).
NOTE: If your enemy is a CharacterController, you should use $$anonymous$$ove or Simple$$anonymous$$ove to move it:
GetComponent(CharacterController).Simple$$anonymous$$ove(myTransform.forward * dir * moveSpeed);
You should pass the velocity vector to Simple$$anonymous$$ove (gravity is automatically applied).
Your answer
Follow this Question
Related Questions
SphereCastAll sees for miles 2 Answers
How can I to tell if something blocks the vision of my enemy? 1 Answer
Why is this script causing a lot of lag? 2 Answers
How to make an object jump? 0 Answers
switch prefab on collision 1 Answer