- Home /
i fixed the issue but none the answer worked so can't accept it, it'll be difficult for others, just closing it
how to stop character going into wall lmesh when moving forward
hello there, i am trying to make 2.5D game with a cube. i am facing this weird problem when the cube is moving forward it's some part of mesh goes inside Wall object mesh also when jumping it's sticking with wall and if i kept pressing moveforward button it get's pass the wall. I tried few solutions i found on internet like apply physics material with 0 friction on wall, and use Rigidbody.MovePostion instead of transform.Translate but none worked, so i am asking here https://www.youtube.com/watch?v=Z7kRpsiJUW0&feature=youtu.be (video of problem)
code for movement
void Update()
{
float ver = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
move = new Vector3(0, 0, ver);
// move = (transform.forward * speed * Time.deltaTime);
RIGIDBODY.MovePosition(transform.position + move);
}
code for playerJump
public class PlayerJump : MonoBehaviour
{
[SerializeField]
protected float JumpForce;
[SerializeField]
protected float FallMultiplier;
[SerializeField]
protected float lowJumpGravity;
public bool IsGrounded;
public bool CanDoubleJUmp;
public bool Dowalljump;
public BoxCollider dectorCollider;
public CharacterControl characterControl;
void Start()
{
characterControl = GetComponent<CharacterControl>();
}
void Update()
{
WallJumping();
if (Input.GetKeyDown(KeyCode.Space))
{
if (IsGrounded)
{
characterControl.Jump = true;
CanDoubleJUmp = true;
}
if (!IsGrounded)
{
if (CanDoubleJUmp)
{
characterControl.DoubleJump = true;
}
}
}
if (Input.GetKey(KeyCode.Return))
{
Dowalljump = true;
}
}
void FixedUpdate()
{
IsGrounded = false;
ApplyGravity();
Jump();
}
void ApplyGravity()
{
//if character is falling increase acceraltion
if (characterControl.RIGIDBODY.velocity.y < 0f)
{
characterControl.RIGIDBODY.velocity += Vector3.up * Physics.gravity.y * (FallMultiplier - 1) * Time.deltaTime;
}
//if it's in air make him fall don't keep going up
else if (characterControl.RIGIDBODY.velocity.y > 0f && characterControl.Jump == false)
{
characterControl.RIGIDBODY.velocity += Vector3.up * Physics.gravity.y * (lowJumpGravity - 1) * Time.deltaTime;
}
}
void Jump()
{
if (characterControl.Jump == true)
{
characterControl.RIGIDBODY.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
}
if (characterControl.DoubleJump == true)
{
CanDoubleJUmp = false;
characterControl.RIGIDBODY.AddForce(Vector3.up * 6f, ForceMode.Impulse);
}
characterControl.DoubleJump = false;
characterControl.Jump = false;
}
void OnTriggerStay(Collider GroundColider)
{
if (!GroundColider.isTrigger && GroundColider.gameObject.tag == "Ground")
{
IsGrounded = true;
}
}
void WallJumping()
{
RaycastHit leftHitInfo;
RaycastHit RightHitInfo;
if (Physics.Raycast(transform.position, this.transform.forward, out RightHitInfo, 0.5f))
{
Debug.DrawRay(transform.position, this.transform.forward * RightHitInfo.distance, Color.red);
Debug.Log(RightHitInfo.collider.tag);
if (!IsGrounded && Dowalljump == true)
{
if (RightHitInfo.collider.tag == "RightWall")
{
characterControl.RIGIDBODY.AddForce(RightHitInfo.normal * 6f, ForceMode.Impulse);
characterControl.RIGIDBODY.AddForce(Vector3.up * 8f, ForceMode.Impulse);
}
}
Dowalljump = false;
}
if (Physics.Raycast(transform.position, -this.transform.forward, out leftHitInfo, 0.5f))
{
Debug.DrawRay(transform.position, -this.transform.forward * leftHitInfo.distance, Color.red);
Debug.Log(leftHitInfo.collider.tag);
if (!IsGrounded && Dowalljump == true)
{
if (leftHitInfo.collider.tag == "LeftWall")
{
characterControl.RIGIDBODY.AddForce(leftHitInfo.normal * 6f, ForceMode.Impulse);
characterControl.RIGIDBODY.AddForce(Vector3.up * 8f, ForceMode.Impulse);
}
}
Dowalljump = false;
}
}
}
.
I had the same issue as you.
I fixed it by using CharacterController
ins$$anonymous$$d of Rigidbody
use rigidbody velocity for example, that will fix it
yes it worked but now my character/Cube can't jump also i have to set my speed to 400 to move it, also gravity stoped working too character is falling to slow
i don't like using characontroller movment doesn't look good and with rigidbody you've more control
Answer by lgarczyn · Oct 09, 2019 at 09:18 PM
MovePosition
will go through walls, it needs to be paired with a raycast, most likely a CapsuleCast
in your situation.
If you wish to avoid this kind of issues, simply use AddForce
or set the velocity depending on input.
Problem With Rigidbody.velocity Setting velocity Rigidbody.velocity is working but if i do that, Apply Gravity and jump stops working i thnk i should say they are behaving very weirdly like jump not working and if i set jumpforce value very high like 500 character will teleport in upward direction like snaping, same for gravity character is falling really slow and with snap effect.
Problem with Addforce problem with addforce is that my game is endless runner and addforce give effect like acceleration character starts to move slowly then speed increases slowly i don't want that since it's endless runner
please help i give you my scripts if you like
Set a target velocity so that your character doesn't continue to accelerate after reaching the desired move speed . For example...
float moveSpeed = 5f;
Vector3 direction = Vector3.forward;
Vector3 targetVelocity = direction * moveSpeed;
rigidbody.AddForce(targetVelocity - rigidbody.velocity);
Ok but it looks like Character still start with less velocity than what i set. but it's way better
Use Force$$anonymous$$ode.Impulse to achieve an instant acceleration.
yes that's what i thought it did the work but now same problem as rigidbody.velocity jump and gravity acting weird it's falling slowly jump not working if it's high value like 500f it's snap like effect. also tried Force$$anonymous$$ode.velocityChange, other options
with velocitychange and Impulse same problem with jump and gravity with .Force and acceleration as starting slow problem
You can just add an acceleration multiplier, so that your character accelerates more quickly.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Basic collision not working - c# 1 Answer
,Trouble with Physics.IgnoreColliion 0 Answers
Trying to get this coin system to work to no avail 1 Answer
Distribute terrain in zones 3 Answers