Hi,
im trying to build a playercontrol in the style of the old N64 / PS1 games. The controller is fine but if u turn around and walk to the "user" the camera starts to bug. This only happens if u turn by the angle of 180, if you walk on the side its fine.
I just took a short video of it:
https://sendvid.com/aquy54le
The camera Script is:
I dont think the Error depends on the playerControl because the player moves correctly without the camera Script. Anyway here is the Playercontroller:
Just hoping that someone gets to know what to do here
im trying to build a playercontrol in the style of the old N64 / PS1 games. The controller is fine but if u turn around and walk to the "user" the camera starts to bug. This only happens if u turn by the angle of 180, if you walk on the side its fine.
I just took a short video of it:
https://sendvid.com/aquy54le
The camera Script is:
Code:
public GameObject target;
public float damping = 1;
Vector3 offset;
void Start()
{
offset = target.transform.position - transform.position;
}
void LateUpdate()
{
float currentAngle = transform.eulerAngles.y;
float desiredAngle = target.transform.eulerAngles.y;
float angle = Mathf.LerpAngle(currentAngle, desiredAngle, Time.deltaTime * damping);
Quaternion rotation = Quaternion.Euler(0, angle, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt(target.transform);
}
I dont think the Error depends on the playerControl because the player moves correctly without the camera Script. Anyway here is the Playercontroller:
Code:
// movementSpeed
public float speed = 5;
// jumpSpeed
public float jumpPower = 15;
// multiplier while jumping
public float airMultiplier = 0.25f;
// rotating
protected float rotationSpeed = 450.0f;
// camera object
public Transform cameraTransform;
// velocity
private Vector3 velocity;
// character
private Transform thisTransform;
// char Controller
private CharacterController character;
// character transform
private Transform characterTransform;
// Starting Point
void Start()
{
// Reference
thisTransform = GetComponent<Transform>();
// Reference
character = GetComponent<CharacterController>();
// Reference
characterTransform = character.transform;
}
// Character Look Direction
void FaceMovementDirection()
{
// direction to look
Vector3 targetDirection = character.velocity;
targetDirection.y = 0.0f;
// direction to move
Vector3 moveDirection = characterTransform.forward;
// rotate
if (targetDirection != Vector3.zero)
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotationSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
thisTransform.forward = moveDirection;
}
}
void Update()
{
// inputs
Vector3 movementInputs = new Vector3(0, 0, 0);
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
movementInputs.z = 1.0f;
}
else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
movementInputs.z = -1.0f;
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
movementInputs.x = -1.0f;
}
else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
movementInputs.x = 1.0f;
}
// movement
Vector3 movement = cameraTransform.TransformDirection(movementInputs);
movement.y = 0;
movement.Normalize();
movement *= speed * ((movementInputs.x != 0.0f || movementInputs.z != 0.0f) ? 1.0f : 0.0f);
// ground check
if (character.isGrounded)
{
// jump
if (Input.GetKeyDown(KeyCode.Space))
{
velocity = character.velocity;
velocity.y = jumpPower;
}
}
else
{
// get back on the ground
velocity.y += Physics.gravity.y * Time.deltaTime;
movement.x *= airMultiplier;
movement.z *= airMultiplier;
}
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
// move
character.Move(movement);
// don't move in the air
if (character.isGrounded)
velocity = Vector3.zero;
// rotate the character
FaceMovementDirection();
}
Just hoping that someone gets to know what to do here