Hi
i’m trying to control a 3D Character in unity. i can move for about a couple seconds before the character goes wild with rotation and flying etc.
removed the jump script and it got slightly better but still goes crazy… i’m working on this problem for about a week now and i’m getting desperate… (yes, code is all over the place, i know. still trying to solve it)
I’m not allowed to embed pictures so here are screenshots. first one has rotation with it
first part, right side camera rotation, left movement
second part movement. all beyond is as a comment
(edit)
PlayerMovement.cs
characterMovement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
CharacterController controller;
[SerializeField] //Ist im Inspector sichtbar
[Range(1, 360)] //Schieberegler
private float speed;
private float speedBackup;
private Vector3 gravity = new Vector3(0, -9.81f, 0);
[SerializeField] //Ist im Inspector sichtbar
[Range(1, 12)] //Schieberegler
private float gravityModifier;
//rotation
[SerializeField] //Ist im Inspector sichtbar
[Range(0, 360)] //Schieberegler
private float speedRotation;
[SerializeField] //Ist im Inspector sichtbar
[Range(0.5f, 600)] //Schieberegler
private float jumpPowerBase;
private float jumpPowerCurr;
void Start()
{
controller = this.GetComponent<CharacterController>();
jumpPowerCurr = 0;
speedBackup = speed;
}
void Update()
{
Cursor.lockState = CursorLockMode.Locked;
if (Input.GetKey(KeyCode.LeftShift)) speed = speedBackup * 1.7f;
else speed = speedBackup;
if (Input.GetKey(KeyCode.W)) controller.Move(Vector3.forward * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.S)) controller.Move(Vector3.back * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.A)) controller.Move(Vector3.left * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D)) controller.Move(Vector3.right * speed * Time.deltaTime);
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
Vector3 motion = new Vector3(horizontal, 0, vertical) * speed;
var gravityVelocity = gravity;
var jumpVelocity = Vector3.up * jumpPowerCurr;
controller.Move(transform.rotation * (motion /* + jumpVelocity + gravity*/) * Time.deltaTime);
//Rotation
float mouseHorizontal = Input.GetAxis("Mouse X");
Vector3 rotate = new Vector3(0, mouseHorizontal, 0) * speedRotation * Time.deltaTime;
this.transform.Rotate(rotate);
}
}
CameraXRotation.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraXRotation : MonoBehaviour
{
[SerializeField] float rotationXSpeed;
[SerializeField] float LerpRotationXSpeed;
void Update()
{
float mouseVertical = Input.GetAxis("Mouse Y");
// Lerped Rotation
Quaternion from = transform.rotation;
Quaternion to = transform.rotation * Quaternion.Euler(-mouseVertical, 0, 0);
transform.rotation = Quaternion.Lerp(from, to, LerpRotationXSpeed * Time.deltaTime);
// Strict Rotation
//Vector3 rotation = new Vector3(-mouseVertical,0,0) * rotationXSpeed * Time.deltaTime;
//transform.Rotate(rotation);
}
}