- Home /
Question by
CAwesome · Dec 23, 2014 at 07:08 PM ·
movementcharactercontrollertrajectory
Achieving 3d curved trajectory with CharacterController
I have a platformer where I would like the controller be able to jump in trajectories, depending on what the slop is when they are exiting.
I would like this behavior![alt text][1]
but all my tries have resulted in this ![alt text][2]
I have tried using both SimpleMove and Move. The latest try was to check if grounded. If so, calculate the slope manually every update, and modify the vector based on that. When midair, it would simply continue with the exception of gravity.
In Update:
private void Update ()
{
CharacterController controller = GetComponent<CharacterController> ();
Vector3 forward = getForwardDirectionWithSlope();
float speed = speedPlayerShouldHave ();
if (controller.isGrounded) {
controller.SimpleMove (forward * speed);
} else {
Vector3 currentVelocity = controller.velocity;
currentVelocity.y -= 9.81f * Time.deltaTime;
controller.Move (currentVelocity * Time.deltaTime);
}
}
private Vector3 getForwardDirectionWithSlope ()
{
RaycastHit hit;
Ray downRay = new Ray (transform.position, -Vector3.up);
Vector3 forwardVector = transform.TransformDirection (Vector3.forward); //default forward;
CharacterController controller = GetComponent<CharacterController> ();
if (Physics.Raycast (downRay, out hit) && controller.isGrounded) {
Vector3 diffVector = hit.point - lastContactPoint;
diffVector.Normalize ();
forwardVector.y = diffVector.y;
forwardVector.Normalize ();
lastContactPoint = hit.point;
}
return forwardVector;
}
I am using CharacterController since the game is VR, and it was pretty easy to set up. If there is some other way, I'm idle ears. [1]: /storage/temp/37508-unityanswer1.png [2]: /storage/temp/37509-unityanswer2.png
unityanswer1.png
(29.9 kB)
unityanswer2.png
(29.3 kB)
Comment