- Home /
Question by
Richard-Tec · Apr 14, 2020 at 12:06 PM ·
collisionmovementphysicscontrollercollision detection
Every time my character hits or touches the walls he gets stuck or acts as if the object the character hit was a magnet.
Hi, I'm new in Unity and I'm creating my first game. When I was testing it, I ran into a problem, every time my character leaned against a wall he got stuck to it, and if I tried to move, he slowly walked as if he was being drawn to the obstacles.
private void Update()
{
if (!photonView.IsMine)
{
RefreshMultiplayerState();
return;
}
//Axis
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
//Controls
bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
bool crouch = Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl);
bool pause = Input.GetKeyDown(KeyCode.Escape);
//States
bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.15f, ground);
bool isJumping = jump && isGrounded;
bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
bool isCrouching = crouch && !isSprinting && !isJumping && isGrounded;
//pause
if (pause)
{
GameObject.Find("Pause").GetComponent<Pause>().TogglePause();
}
if (Pause.paused)
{
t_hmove = 0f;
t_vmove = 0f;
sprint = false;
jump = false;
crouch = false;
pause = false;
isGrounded = false;
isJumping = false;
isSprinting = false;
isCrouching = false;
}
//crouching
if (isCrouching)
{
photonView.RPC("SetCrouch", RpcTarget.All, !crouched);
}
//Jumping
if (isJumping)
{
if (crouched) photonView.RPC("SetCrouch", RpcTarget.All, false);
rig.AddForce(Vector3.up * jumpForce);
}
if (Input.GetKeyDown(KeyCode.U)) TakeDamage(50);
//UI Refreshes
RefreshHealthBar();
weapon.RefreshAmmo(ui_ammo);
}
// Update is called once per frame
void FixedUpdate()
{
if (!photonView.IsMine) return;
//Axis
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
//Controls
bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
bool slide = Input.GetKey(KeyCode.LeftControl);
//States
bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.1f, ground);
bool isJumping = jump && isGrounded;
bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
bool isSliding = isSprinting && slide && !sliding;
//pause
if (Pause.paused)
{
t_hmove = 0f;
t_vmove = 0f;
sprint = false;
jump = false;
isGrounded = false;
isJumping = false;
isSprinting = false;
}
//Movement
Vector3 t_direction = Vector3.zero;
float t_adjustedSpeed = speed;
if (!sliding)
{
t_direction = new Vector3(t_hmove, 0, t_vmove);
t_direction.Normalize();
t_direction = transform.TransformDirection(t_direction);
if (isSprinting)
{
if (crouched) photonView.RPC("SetCrouch", RpcTarget.All, false);
t_adjustedSpeed *= sprintModifier;
}
else if (crouched)
{
t_adjustedSpeed *= crouchModifier;
}
}
else
{
t_direction = slide_dir;
t_adjustedSpeed *= slideModifier;
slide_time -= Time.deltaTime;
if(slide_time <= 0)
{
sliding = false;
weaponParentCurrentPos -= Vector3.down * (slideAmount - crouchAmount);
}
}
Vector3 t_targetVelocity = t_direction * t_adjustedSpeed * Time.deltaTime;
t_targetVelocity.y = rig.velocity.y;
rig.velocity = t_targetVelocity;
//sliding
if (isSliding)
{
sliding = true;
slide_dir = t_direction;
slide_time = lengthOfSlide;
//adjust camera
weaponParentCurrentPos += Vector3.down * (slideAmount - crouchAmount);
if (!crouched) photonView.RPC("SetCrouch", RpcTarget.All, true);
}
//Field of View and camera stuff
if (sliding)
{
normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier * 1.2f, Time.deltaTime * 8f);
normalCam.transform.localPosition = Vector3.Lerp(normalCam.transform.localPosition, origin + Vector3.down * slideAmount, Time.deltaTime * 6f);
}
else
{
if (isSprinting) { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier, Time.deltaTime * 8f); }
else { normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV, Time.deltaTime * 8f); }
if(crouched) normalCam.transform.localPosition = Vector3.Lerp(normalCam.transform.localPosition, origin + Vector3.down * crouchAmount, Time.deltaTime * 6f);
else normalCam.transform.localPosition = Vector3.Lerp(normalCam.transform.localPosition, origin, Time.deltaTime * 6f);
}
}
Comment