Movement of a sphere
Hello guys, i am making a game where the player is a sphere. The view is in 3rd person and i am using the Cinemachine Tool for it. I am currently stuck. 1. I have no clue how i can rotate the sphere in the movement direction. 2. When Y of the Player is not 0 then the speed is not constant. It means im pressing ("w"), but he goes with (6,0,0.3) then i move my mouse and the speed is (5,8,0,0.7). With speed i mean the Vector3 values i calculate with the IEnumerator.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController Controller;
public Transform cam;
public GameObject PlayerBody;
public GameObject Sphere;
public GameObject ThirdPersonCam;
public float Speed = 0.01f;
public float xRotation;
public float zRotation;
Vector3 prevPos;
Vector3 currVel;
void Start()
{
StartCoroutine(CalcVelocity());
}
public float TurnSmoothTime = 0.1f;
float TurnSmoothVelocity;
private void Update()
{
float Horizontal = Input.GetAxisRaw("Horizontal");
float Vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(Horizontal, 0f, Vertical).normalized;
Cursor.lockState = CursorLockMode.Locked;
//Debug.Log(Horizontal + " "+ Vertical);
if (direction.magnitude >= 1f)
{
float TargetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
Vector3 MoveDirection = Quaternion.Euler(0f, TargetAngle, 0f) * Vector3.forward;
//transform.rotation = Quaternion.Euler(0f, TargetAngle, 0f);
Controller.Move(MoveDirection.normalized * Speed * Time.deltaTime);
xRotation = 2 * Mathf.PI * Speed * Horizontal + PlayerBody.GetComponent<Transform>().rotation.x;
zRotation = 2 * Mathf.PI * Speed * Vertical + PlayerBody.GetComponent<Transform>().rotation.z;
}
PlayerBody.GetComponent<Transform>().rotation = Quaternion.Euler(xRotation, ThirdPersonCam.GetComponent<CinemachineFreeLook>().m_XAxis.Value, zRotation);
}
IEnumerator CalcVelocity()
{
while (Application.isPlaying)
{
// Position at frame start
prevPos = transform.position;
// Wait till it the end of the frame
yield return new WaitForEndOfFrame();
// Calculate velocity: Velocity = DeltaPosition / DeltaTime
currVel = (prevPos - transform.position) / Time.deltaTime;
Debug.Log(currVel);
}
}
}
This is my project
Your answer
Follow this Question
Related Questions
Strange rotation behaviour 0 Answers
Camera Rotation every time a key is pressed 0 Answers
How can I make the camera not overreach a limit of rotation one axis. 0 Answers
How can I determine which direction the player is moving based on mouse rotation direction? 1 Answer
Problem with player direction using virtual joysticks 0 Answers