using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour
{
public float speed = 5.0f;
public float rotationSpeed;
public float jumpForce = 5;
public bool isOnGround = true;
public float horizontalInput;
public float forwardInput;
private Rigidbody playerRb;
public float CharacterrotationHorizontal = 0f;
// Transform Player;
// Start is called before the first frame update
void Start()
{
playerRb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// get player input
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// Move the player foward
// transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput); // - up and down
// transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput); //left and right
Vector3 movementDirection = new Vector3(horizontalInput, 0); //- This for left and right
//Vector3 movementDirection = new Vector3(verticalInput, horizontalInput, 0);
movementDirection.Normalize();
transform.Translate(movementDirection * Time.deltaTime * speed, Space.World);
if (movementDirection != Vector3.zero)
{
Quaternion toRotation = Quaternion.LookRotation(horizontalInput * Vector3.right, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
CharacterrotationHorizontal = Mathf.Clamp(CharacterrotationHorizontal, -180f, 180f);
//Player(transform.rotation, Vector3.up * CharacterrotationHorizontal);
print(CharacterrotationHorizontal);
}
//Let the player jump
if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isOnGround = false;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isOnGround = true;
}
}
}
Are you just writing Yet Another Character Controller?? Do you really want to hassle with writing your own CC? It’s pretty difficult obtuse stuff, certainly NOT a beginner topic at all.
If so, tuck in and prepare to do some deep debugging. Meanwhile, if you just want one to use in your game and move forward, here’s two:
Here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):
That one has run, walk, jump, slide, crouch.
And here is a free Kinematic Character Controller that also comes with a huge playground:
Is the root ‘Player’ game object rotated (potentially by 90 degrees)?
It would mean rotating the player to world right/left would give the impression its rotating 180 degrees, but it’s only 180 degrees locally. Remember the inspector for the transform shows local values, and not world ones.