- Home /
How to move Character Controller from point-to-point absolutely
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. :)
Without your script and more information, it is difficult to give a focused answer. Here is some logic that might be in the right direction.
var dir = lanePosition - transform.position;
var moveDir = dir.normalized * Time.deltaTime * speed;
if (moveDir.sqr$$anonymous$$agnitude > dir.sqr$$anonymous$$agnitude) {
moveDir = dir;
}
With forward movement and lane changes, your code will be more complicated. Posting your code might help someone give a focused answer to your question.
Answer by robertbu · Dec 18, 2013 at 04:32 AM
Looking at just MoveToPoint() and taking the logic I posted into the comment and integrating it into your code:
void MoveToPoint()
{
if (targetPosition == transform.position)
return;
Vector3 movDiff = targetPosition - transform.position;
Vector3 movDir = moveDiff.normalized * 15f * Time.deltaTime;
if(moveDir.sqrMagnitude < moveDiff.sqrMagnitude)
{
dogCtrl.Move(movDir);
}
else
{
dogCtrl.Move(movDiff)
}
}
If I understand this right, we move the character to the target position until the position of the character is equal to the target position. I'm quite confused about the dogCtrl.$$anonymous$$ove(movDiff). Does that mean that if the character exceeds the target position, we move back?
'movDiff' is a vector from the current position to the end position. 'movDir' calculated move for the frame based on deltaTime. This code says that if a move the whole way (movDiff) is shorter than 'movDir', then we use the shorter move. In other words, if using the calculate move (movDir) would overshoot the end position, we use the shorter move directly to the end position.
This works perfectly. One problem left is that the z value here is somehow stuck until the end of the lane change (if I'm right). The running and change lane script runs separately. How can I tweak this. Should I just combine the script and do the move calculation once?
Since you move over time, I'd update the 'z' parameter of targetPosition each frame. One script or two depends on the structure of your game.