- Home /
Move in look direction with PlayerController
Hello,
I have a character that moves on a splinebased path. I have a method that calculates my look rotation based on my current position on the path (I get a point that is just before me on the path. So i am able to always look in the direction i am heading).
Is it possible to use the CharacterController / CharacterMotor to move in the direction i am looking? My look rotation is updated in every update cycle. So the movment direction must be adjusted every frame as well. I have tried this before by myself but got really weird results.
Here is the code for the recalculation of the rotation. (There is code used from the iTween examples mixed with my own code and method calls from whydoidoit´s Splinemovement):
/*
* pathPosition = percentage my character already travelled on the path
* lookAheadAmount = the percentage i want to add every cycle to my current pathPosition
* (how far i want to look ahead as a percentage)
* lookPercentage = combination of lookAheadAmount and pathPosition thus
* the percentage of the point that i want to look at
*
* lookTarget = a Vector3. Its the point before me i want to look at
*/
private void FindRotation ()
{
float lookPercentage;
//calculate look data if we aren't going to be looking beyond the extents of the path:
//leading or trailing point so we can have something to look at:
if (pathPosition + lookAheadAmount <= 1 && characterDirection == Direction.Forward) {
//if i walk forward I need to add the lookAheadAmount
lookPercentage = pathPosition + lookAheadAmount;
//MoveOnPath transforms my percentage into a Vector3
lookTarget = Spline.MoveOnPath (path, transform.position, ref lookPercentage,maxPlayerSpeed, globalSmooth, EasingType.Linear, false, false);
transform.LookAt (lookTarget);
//nullify all rotations but y since we just want to look where we are going:
float yRot = transform.eulerAngles.y;
transform.eulerAngles = new Vector3 (0, yRot, 0);
}
if (pathPosition - lookAheadAmount >= 0 && characterDirection == Direction.Reverse) {
// if i move backwards i need to substract my lookAhead amount
lookPercentage = pathPosition - lookAheadAmount;
//MoveOnPath transforms my percentage into a Vector3
lookTarget = Spline.MoveOnPath (path, transform.position, ref lookPercentage,maxPlayerSpeed, globalSmooth, EasingType.Linear, false, false);
transform.LookAt (lookTarget);
//nullify all rotations but y since we just want to look where we are going:
float yRot = transform.eulerAngles.y;
transform.eulerAngles = new Vector3 (0, yRot, 0);
}
}
It would be awesome if I could use the character controller/ charactermotor because I must not worry about collisions, jumping and sliding.
you don' t need character motor , but you can use third person controller. and its camera, it rotates the Vector3.fwd property of the player then use input to move in that direction, it will then rotates the player to move the way.
I am currently trying to do what you said. If the question is not clear enough i will explain it further. :)
We need to see the move code, not the rotation code to solve this problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123017im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Default Character Motor randomly jumping? 1 Answer
Spline with character enter 0 Answers
Character Motor won't go up stairs. 6 Answers