Question by
Sapien_ · Apr 26, 2018 at 09:58 PM ·
rotationscript.charactercontrollerdirectionwalljump
Controller Rotation returns to Zero when there is no movement.
I have managed to get my character to face the direction he is moving in, however when the character is not moving, He faces his start position. All of the rotation values return to 0. Why does this happen?
Also how do I get my character to face the direction of my inputs in the air?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
public class Aerial_Controler : MonoBehaviour {
public CharacterController MyController;
// Update is called once per frame
public float GroundSpeed = 7f;
public float CurrentSpeed;
public float SprintSpeed = 10f;
public float TimeZeroToMax = 2.5f;
float accelRatePerSec;
public float AerialSpeed = 2f;
public float GravityStrength = 5f;
public float JumpSpeed = 10f;
public Transform CameraTransform;
bool canJump = false;
float verticalVelocity;
Vector3 velocity;
Vector3 groundedVelocity;
Vector3 normal;
bool onWall = false;
private void Start()
{
accelRatePerSec = SprintSpeed / TimeZeroToMax;
CurrentSpeed = 0f;
MyController = GetComponent<CharacterController>();
}
void Update()
{
bool run = Input.GetKey(KeyCode.M);
if (run)
{
CurrentSpeed += accelRatePerSec * Time.deltaTime;
CurrentSpeed = Mathf.Min(CurrentSpeed, SprintSpeed);
}
else
{
CurrentSpeed = GroundSpeed;
}
if (Input.GetKeyUp(KeyCode.M))
{
CurrentSpeed += accelRatePerSec * Time.deltaTime;
CurrentSpeed = Mathf.Max(GroundSpeed, CurrentSpeed);
}
if (run && !MyController.isGrounded)
{
CurrentSpeed = GroundSpeed + AerialSpeed;
}
Physics.IgnoreLayerCollision(9, 10);
Vector3 myVector = Vector3.zero;
//get input from the player
Vector3 input = Vector3.zero;
input.x = Input.GetAxis("Horizontal");
input.z = Input.GetAxis("Vertical");
input = Vector3.ClampMagnitude(input, 1f);
transform.rotation = Quaternion.LookRotation(groundedVelocity);
if (MyController.isGrounded)
{
myVector = input;
Quaternion inputRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(CameraTransform.forward, Vector3.up), Vector3.up);
myVector = inputRotation * myVector; //rotate input by direction of the camera
myVector *= CurrentSpeed;
}
else
{
Quaternion inputRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(CameraTransform.forward, Vector3.up), Vector3.up);
input = inputRotation * input;
myVector = groundedVelocity;
myVector += input * AerialSpeed;
}
myVector = Vector3.ClampMagnitude(myVector, CurrentSpeed);
myVector = myVector * Time.deltaTime;
verticalVelocity = verticalVelocity - GravityStrength * Time.deltaTime;
if (Input.GetButtonDown("Jump"))
{
if (onWall)
{
Vector3 reflection = Vector3.Reflect(velocity, normal);
Vector3 projected = Vector3.ProjectOnPlane(reflection, Vector3.up);
groundedVelocity = projected.normalized * CurrentSpeed + normal * AerialSpeed;
}
//add Jumped to vertical velocity
if (canJump)
verticalVelocity += JumpSpeed;
}
myVector.y = verticalVelocity * Time.deltaTime; //add new speed, to old speed, for acceleration
//use input to move the character
CollisionFlags flags = MyController.Move(myVector);
velocity = myVector / Time.deltaTime;
print(velocity);
//use flags to determine if character can jump or not
//if on ground
//set canJump to true
if ((flags & CollisionFlags.Below) != 0)
{
groundedVelocity = Vector3.ProjectOnPlane(velocity, Vector3.up);
canJump = true;
verticalVelocity = -3f;
onWall = false;
}
else if ((flags & CollisionFlags.Sides) != 0)
{
canJump = true;
onWall = true;
}
else
{
canJump = false;
onWall = false;
}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
normal = hit.normal;
}
}
Comment
Wow i forgot about this. I still haven't figured it out. All I know is that it has something to do with the Quaternion.LookRotation.
Your answer
Follow this Question
Related Questions
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers
Sliding in only one direction 1 Answer
Rotation problems 0 Answers
Help with player rotation on rotating sphere 1 Answer
How can I incorporate a Rotation Towards the Mouse Position into this Script? I Tried. 0 Answers