Issue with player moving forward through collider
Hi all, This is my first time posting to this forum, so thanks in advance to all who read and hopefully respond. Im slowly following a tutorial to create a climbing system for a third person game, i wish to use the basic functions of this as the basis of a climbing game. I'm trying to get the player to climb a simple cube, the controls for movement are GetInputAxis, hor and vert, i can see they are returning values in the debug log. The character is correctly Lerping to the face of the cube at a predefined height, however as soon as I move the arrow keys the player is moving in the correct direction but also moving forward and getting stuck in the cube object. I've spent two days trying to make changes to affect this but I'm having no joy. I understand some but not all of the C# code that I've been building (with the help of a brilliant Sharp Accent tutorial - BOTW Climbing) The player should be moving relative to the face of the cube, but instead the player is always moving forward when i press up, down, left or right and then freezing.
Any help greatly appreciated! i guess i just want to know how to disable the player from moving forward into a collider when the player is in climbing position.
Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SA
{
public class FreeClimb : MonoBehaviour
{
public bool isClimbing;
public Animator anim;
public bool inPosition;
public bool isLerping;
float t;
Vector3 startPos;
Vector3 targetPos;
Quaternion startRot;
Quaternion targetRot;
public float positionOffset;
public float offsetFromWall = 0.45f;
public float speed_multiplier = 0.2f;
public float climbSpeed = 3;
public float rotateSpeed = 5;
public float inAngleDis = 1;
public float horizontal;
public float vertical;
Transform helper;
float delta;
void Start()
{
Init();
}
public void Init()
{
helper = new GameObject().transform;
helper.name = "climb helper";
CheckForClimb();
}
public void CheckForClimb()
{
Vector3 origin = transform.position;
origin.y += 1.4f;
Vector3 dir = transform.forward;
RaycastHit hit;
if(Physics.Raycast(origin,dir, out hit, 2))
{
helper.position = PosWithOffset(origin, hit.point);
InitForClimb(hit);
}
}
void InitForClimb(RaycastHit hit)
{
isClimbing = true;
helper.transform.rotation = Quaternion.LookRotation(-hit.normal);
startPos = transform.position;
targetPos = hit.point + (hit.normal * offsetFromWall);
t = 0;
inPosition = false;
anim.CrossFade("climb_idle", 1);
}
void Update()
{
delta = Time.deltaTime;
Tick(delta);
}
public void Tick(float delta)
{
if(!inPosition)
{
GetInPosition();
return;
}
if(!isLerping)
{
horizontal = Input.GetAxis("horizontal");
vertical = Input.GetAxis("vertical");
float m = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
Vector3 h = helper.right * horizontal;
Vector3 v = helper.up * vertical;
Vector3 moveDir = (h + v).normalized;
bool canMove=CanMove(moveDir);
if (!canMove || moveDir == Vector3.zero)
return;
t = 0;
isLerping = true;
startPos = transform.position;
Vector3 tp = helper.position - transform.position;
targetPos = helper.position;
}
else
{
t += delta * climbSpeed;
if(t > 1)
{
t = 1;
isLerping = false;
}
Vector3 cp = Vector3.Lerp(startPos, targetPos, t);
transform.position = cp;
transform.rotation = Quaternion.Slerp(transform.rotation, helper.rotation, delta * rotateSpeed);
}
}
bool CanMove(Vector3 moveDir)
{
Debug.Log(moveDir);
Vector3 origin = transform.position;
float dis = positionOffset;
Vector3 dir = moveDir;
Debug.DrawRay(origin, dir * dis, Color.red);
RaycastHit hit;
if(Physics.Raycast(origin,dir, out hit ,dis))
{
return false;
}
origin += moveDir * dis;
dir = helper.forward;
float dis2 = inAngleDis;
Debug.DrawRay(origin, dir * dis2, Color.blue);
if(Physics.Raycast(origin,dir, out hit, dis))
{
helper.position = PosWithOffset(origin, hit.point);
helper.rotation = Quaternion.LookRotation(-hit.normal);
Debug.Log("f");
return true;
}
origin += dir * dis2;
dir = Vector3.up;
Debug.DrawRay(origin, dir, Color.yellow);
if(Physics.Raycast(origin,dir, out hit, dis2))
{
float angle = Vector3.Angle(helper.up, hit.normal);
if(angle < 40)
{
helper.position = PosWithOffset (origin, hit.point);
helper.rotation = Quaternion.LookRotation(-hit.normal);
return true;
}
}
return false;
}
void GetInPosition()
{
t += delta;
if(t > 1)
{
t = 1;
inPosition = true;
}
Vector3 tp = Vector3.Lerp(startPos, targetPos, t);
transform.position = tp;
transform.rotation = Quaternion.Slerp(transform.rotation, helper.rotation, delta * rotateSpeed);
}
Vector3 PosWithOffset (Vector3 origin, Vector3 target)
{
Vector3 direction = origin - targetPos;
direction.Normalize();
Vector3 offset = direction * offsetFromWall;
return target + offset;
}
}
}