- Home /
Player making Zigzag movement
I using to simple scripts, one for the player and other for the camera.
The player it's a cube walking in a floor, just with the script and the Character controller component.
The camera isn't attached to the cube and his position and rotation it's calculated by the cube's position and rotation.
When I walk to front, back or sidewalking, it's fine, but when I walk to front and side, the player start to shaking, making a zigzag movement.
Isn't the camera. I searched in the google and I found some topics about the camera. But I can see him doing this strange movement on the Scene window, so isn't the camera.
I guess that I need some "smoothing-system" to calcule his position. I don't know.
Can someone help me?
Here are my Scripts:
The Player Script:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
private CharacterController controller;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 Pdirection = Vector3.zero;
public float mouseSens = 0.2f;
private bool locked = true;
void Start () {
Screen.lockCursor = locked;
}
void FixedUpdate () {
controller = GetComponent<CharacterController> ();
if (controller.isGrounded) {
Pdirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
Pdirection = transform.TransformDirection(Pdirection);
Pdirection *= speed;
}
Pdirection.y -= gravity * Time.deltaTime;
print (Pdirection);
controller.Move (Pdirection * Time.deltaTime);
controller.transform.Rotate(0, Input.GetAxis ("Mouse X")*mouseSens*Time.deltaTime, 0);
if (Input.GetButton ("MouseLock")) {
locked = !locked;
print(locked);
Screen.lockCursor = locked;
}
}
}
The Camera Script:
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour {
public float distancia = 10;
public Transform target;
void Start () {
}
void Update () {
if (!target)
return;
float anguloFinal = target.transform.eulerAngles.y;
float distanciaFinal = target.transform.position.y + distancia;
float anguloAtual = transform.eulerAngles.y;
float distanciaAtual = transform.position.y;
anguloAtual = Mathf.LerpAngle (anguloAtual, anguloFinal, Time.deltaTime*0.1f);
distanciaFinal = Mathf.Lerp (distanciaAtual, distanciaFinal, Time.deltaTime * 0.1f);
Quaternion angulo = Quaternion.Euler (0, anguloFinal, 0);
Vector3 posicaoFinal = transform.position;
posicaoFinal = target.transform.position;
posicaoFinal -= angulo * Vector3.forward * distancia;
posicaoFinal.y = distanciaFinal;
transform.position = posicaoFinal;
transform.LookAt (target);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613151537im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
All Unity Engine games' controls not working 1 Answer
How to limit the forces applied to an object to just forward, left, and right? 0 Answers
Player shaking when move (not the camera) 0 Answers
how do i move my player left and right using touch pad? 1 Answer