I’m working on a game like Subway Surf. I’m having trouble when my character needs to change lane. It won’t stop at the absolute point of the target lane. If I use the Translate to snap the character to the target position, it ignores the colliders resulting my character to go through the walls. So I used Move but can’t seem to get it right. Can someone help me with this?
EDIT: So here’s my workaround. The script for changing lane and the running movement were in a separate script.
public class ChangeLane : MonoBehaviour {
public bool toChangeLane, toLeft, toRight;
public bool isChangingLane;
public Vector3 targetPosition;
public float laneDistance;
private CharacterController dogCtrl;
void Awake()
{
dogCtrl = GetComponent<CharacterController>();
targetPosition = new Vector3(transform.position.x - 2, transform.position.y, transform.position.z);
}
void Update()
{
if(toChangeLane)
{
if(toLeft)
{
targetPosition = new Vector3(transform.position.x - laneDistance, transform.position.y, transform.position.z);
toLeft = false;
}
else if(toRight)
{
targetPosition = new Vector3(transform.position.x + laneDistance, transform.position.y, transform.position.z);
toRight = false;
}
isChangingLane = true;
toChangeLane = false;
}
else if(isChangingLane)
{
MoveToPoint();
}
}
void MoveToPoint()
{
Vector3 movDiff = targetPosition - transform.position;
if(movDiff.magnitude > .1f)
{
movDiff = movDiff.normalized * 15f;
dogCtrl.Move(movDiff * Time.deltaTime);
}
else
{
transform.position = targetPosition;
isChangingLane = false;
}
Debug.Log(movDiff.magnitude);
//targetPosition = new Vector3(targetPosition.x, transform.position.y, transform.position.z);
}
}
And here is the snippet from my controller that processes the running.
...
Vector3 movement = moveDirection * runningSpeed + new Vector3 (0f, jumpingSpeed * 3f, 0f);
//moveDirection = forward
movement *= Time.deltaTime;
collisionFlags = dogCtrl.Move(movement);
...
It works fine except:
-
The character falls under the ground and jump right over the target position with the code below uncommented
targetPosition = new Vector3(targetPosition.x, transform.position.y, transform.position.z); -
The character swipes to the target lane a few distance backwards. It’s so noticeable since my camera follows my character
-
And yeah, the character goes through the wall if I manipulate the transform instead of Move because my goal is to bounce back from the former lane if the character collided with an obstacle halfway its changing lane.
-
The final position is not absolute so you can see the character not aligned in the center of the lane.
I can add more details if this is not enough and thank you for any response.