Код передвижения
Синтаксис:
Используется csharp
public class PlayerMovement : MonoBehaviour
{
public CharacterController cc;
Vector3 movement;
public float speed = 5.0f;
private float X;
private float Z;
void Start()
{
cc = GetComponent<CharacterController>();
}
void Update()
{
X = Input.GetAxis("Horizontal") * speed;
Z = Input.GetAxis("Vertical") * speed;
movement = new Vector3(X, 0, Z);
movement = Vector3.ClampMagnitude(movement, speed);
movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
cc.Move(movement);
}
}
{
public CharacterController cc;
Vector3 movement;
public float speed = 5.0f;
private float X;
private float Z;
void Start()
{
cc = GetComponent<CharacterController>();
}
void Update()
{
X = Input.GetAxis("Horizontal") * speed;
Z = Input.GetAxis("Vertical") * speed;
movement = new Vector3(X, 0, Z);
movement = Vector3.ClampMagnitude(movement, speed);
movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
cc.Move(movement);
}
}
Код поворота камеры
Синтаксис:
Используется csharp
public class PlayerRotation : MonoBehaviour
{
public enum RotationAxes
{
MouseXAndY = 0,
MouseX = 1,
MouseY = 2
}
public Rigidbody rb;
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivity = 9.0f;
public float minimumVert = -45.0f;
public float maximumVert = 45.0f;
private float _rotationX = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
if (rb != null)
rb.freezeRotation = true;
}
void Update()
{
{
_rotationX -= Input.GetAxis("Mouse Y") * sensitivity;
_rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
float delta = Input.GetAxis("Mouse X") * sensitivity;
float rotationY = transform.localEulerAngles.y + delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
}
{
public enum RotationAxes
{
MouseXAndY = 0,
MouseX = 1,
MouseY = 2
}
public Rigidbody rb;
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivity = 9.0f;
public float minimumVert = -45.0f;
public float maximumVert = 45.0f;
private float _rotationX = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
if (rb != null)
rb.freezeRotation = true;
}
void Update()
{
{
_rotationX -= Input.GetAxis("Mouse Y") * sensitivity;
_rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
float delta = Input.GetAxis("Mouse X") * sensitivity;
float rotationY = transform.localEulerAngles.y + delta;
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
}
}