First time posting in the forums, I asked this question in Unity Answers but it didn’t help much so wanted to see what you all thought.
I don’t know if anyone knows what I mean when I’m looking for a dodge side step thing in a first person game because I can’t think of any examples. Basically when the player double taps a direction, they jump to the left or right to avoid a collision.
My code works but the the FPC can move through walls when doing this. Anyone have any ideas on what’s a good way to fix this or have a better idea on how to execute this?
if input.getkey (A )
if(CheckColl == true)
call Dodge(5,vector3.left)
else
// donothing or
call Dodge(length to wall ,vector3.left)
and here is check coll function..
function CheckColl()
{
do
Physics.Raycast
if raycast didnt hit wall
return true
}
Dodge function can be something like
function Dodge(var length : float, var dir : vector3)
{
// transform.translate to dir and use length
}
Basicly target ray to that direction where you are going to step, if raycast hits wall you can
call dodge and calculate move length by looking how long raycast moved before hitting wall
Just ignore dodge key if wall is too near ( look code )
Hope this helps
It works still but I couldn’t figure out how to make it check to see if there was a wall there or not so I tried to go off distance to see if there was something there. It didn’t work. And I measured how much my player traveled when side stepping and it turned out to be 2.67298 units (but checking distance wasn’t working). Is there a way to see if the raycast hit anything in a boolean statement? Nothing I was doing was working
Hi,
print (hit.transform.name); , you can also use .tag. Create new tag ( Wall ) and change your walls tag as “Wall”
now on code use this on CheckCollision function
function CheckCollision () : boolean {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, dodge_direction, hit, Mathf.Infinity, layermask)) {
if (hit.transform.tag == "Wall"){
print ("Hitted wall object!");
two_press = true;
return true;
}
else
{
return false;
}
}
}
Remove that CheckCollision call from line 12 and change line 15 to look this
You had it set up so if the raycast did hit a wall then it would side step so I changed that.
Then I realized the distance was set to infinity which is fine but it would always detect being near a wall so I set it if the distance was >=3 then side step but it’s not very good. I’d like my character to be able to side step despite being close to a wall otherwise the game loses it’s fluidity and the controls seem stiff because it only works some of the time and my game is very close quarters.
Is there a way to just set it so that if my player does hit a wall it just stops the side stepping process and moves him in the opposite direction slightly like (-Vector3.left) or (Vector3.right)?
I feel like my player is able to move through the walls because he is being constantly pushed in a direction, if I could somehow stop the movement and return to normal controls then it would work perfectly.
Thanks
Yeah there could be huge amount typos, i didnt debug anything ( write from my mind ) . If you like to stop movement you should just force player position to be same on every frame call . ie : transform.position = transform.position. or transform.position.x = transform.position.x keeps X axle intact…
That basicly means
if DISTANCE is higher or equal to 3 →
This line happens
END
See new variable, MinDistanceToWall. Avoid using IF statement if you can, in this case you dont need to raycast infinite ( it takes also resources:… ) But raycasting only to specific distance…
There you go, new function… You should figure out what it does and implent it to your code, i dont think it works if you copy paste
var MinDistanceToWall : float = 3;
function CheckCollision () : boolean {
// Raycast hit store
var hit : RaycastHit;
// Do raycast, between player position and wanted direction, DO it only distance between startpoint and MinDistanceToWall
if (Physics.Raycast (transform.position, dodge_direction, hit, MinDistanceToWall))
{
// Raycast hitted object !
// Lets check that objects transform TAG
if (hit.transform.tag == "Wall"){
// Object has wall tag
print ("Wall is near us, return true");
// now you know that wall IS near
two_press = true;
// return true , that basicly means: CheckCollision == true
return true;
}
else
{
// no wall in this direction within MinDistanceToWall, we return false
return false;
}
}
}
Without looking your code… i would do this :
lets think that default vector is Vector3.left
var dodge_direction : Vector3;
function start()
{
dodge_direction = Vector3.left;
}
function Update()
{
If ( CheckCollision == true )
{
// stop player movement
transform.position = transform.position;
// change default vector aka dodge_direction
dodge_direction = -Vector3.left;
}
}
The first block of code is basically what I already have but with a different distance instead of infinity. I’ve already tried that and if it doesn’t detect anything, it does nothing and my player still won’t side step.
The second block of code doesn’t work either, the player still goes through walls regardless of that code being implemented.
I think this method of side stepping might be a dead end. It doesn’t quite meet the feel of what I want which is basically Unreal Tournament 2004’s side stepping and has proven to be a hassle lol
I’ve never used CharacterController before, with the scripts above, would it stop moving when it comes in contact with the wall you think? Rigidbody would be my first choice if i wasn’t using FPC, but since FPC and rigidbody don’t go together I’m kind of screwed lol. Would have just used rigidbody.AddForce and never have had a problem.
But what is the best way to go about doing this with CharacterController?
Hmm you’re using the First Person Controller which comes with Unity? That actually already uses a character controller. You will want to modify the code of the FPSInputController and CharacterMotor. Those two scripts are what control movement.
I have a separate player movement script that my friend gave to me that has sprint and crouching in it. I don’t know if he made it or not but it makes modifying the CharacterMotor very easy. What function should I use for CharacterController or motor to get the effect i want? I know nothing about it lol
Put your translation vector inside the Move function. The Move function does check for collision, so it won’t just zoom through the wall. The code isn’t really meant to be copy/pasted in, just to give you the idea of how to incorporate it into your own code.
But again, I am totally unfamiliar with CharacterMotor, so there’s a good chance this won’t work. It will work with just a CharacterController, however.
My script already had a character controller variable so I just replaced the transform.Translate with that and it almost works perfect. The player no longer goes through walls but now the side step is taking place on the global axis or whatever. Like regardless of the direction I’m facing, it will side step along the x-axis instead of the direction relative to the direction the player is facing. Any idea on how I could do that? Simple move doesn’t work either
I didn’t understand what you meant at first but I got it. It’s so close to working, right now it works sometimes but most of the time it either stutters and the player doesn’t move or the player moves in a curve or circle instead of a straight line to the left. Here’s what I have:
When it does work, I’m facing the direction that would be forward globally (which my rotation would be (0, 0, 0)) which you can see in the picture I uploaded. I can’t figure out why it’s only working correctly on that rotation.