Player Rotation and movement
I'v been trying to add rotation and movement towards the way the character is facing, without breaking the physics or locking the character. After hours of googling/youtube and experimentation on how to implement this im back for help again >_<. This is the closest the closest i got to it actually working.The character is rotating but its drifting and moving to the way he's facing but the rotation is awful.. Can someone expainto me what im doing wrong and what the best solution is. This is my first script and im trying to understand and figure things out.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
//VARIABLES
//MOVEMENT//SPEED
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] public float runSpeed;
[SerializeField] public float rot;
[SerializeField] public float rotSpeed;
[SerializeField] public float smooth;
public float turnSmoothTime = 0.1f;
//GRAVITY
[SerializeField] private float jumpHeight;
[SerializeField] private float gravity;
public bool isGrounded = true;
// [SerializeField] private float groundCheckDistance;
//[SerializeField] private LayerMask groundMask;
// Variable for Layer for groundcheck
private Vector3 moveDirection;
private Vector3 velocity;
private Animator anim;
Rigidbody rb;
//REFERENCES
private CharacterController controller;
private void Start()
{
//Debug.Log("Start");
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
}
void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.tag == "Plane")
{
isGrounded = true;
print("grounded");
}
}
void OnCollisionExit(Collision theCollision)
{
if (theCollision.gameObject.tag == "Plane");
{
isGrounded = false;
print("not grounded");
}
}
private void Update()
{
//rot += Input.GetAxisRaw("Mouse X") * rotSpeed * Time.deltaTime;
//transform.eulerAngles = new Vector3(0, rot, 0);
//rotation
//Debug.Log("test");
Move();
}
private void fixedUpdate()
{
}
//MOVEMENT
private void Move()
{
float moveZ = Input.GetAxisRaw("Vertical");
float ad = Input.GetAxis("Horizontal");
//float rot = Input.GetAxis("Mouse X");
moveDirection = new Vector3(ad, rb.velocity.y, moveZ);
if(isGrounded == true)
{
if(moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
//Walk
Debug.Log("Walk");
walk();
}
else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
anim.SetFloat("JumpAnim", 0); //Animation stop
//Run
run();
Debug.Log("run");
}
else if(moveDirection == Vector3.zero)
{
//Ìdle
anim.SetFloat("JumpAnim", 0);
idle();
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
//Animation stop
//Jump
jump();
}
}
//transform.rotation =Quaternion.LookRotation(transform.up * rotSpeed * rot);
//rb.AddTorque(transform.up * rotSpeed * rot);
//float targetAngle = Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg;
//float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle,ref smooth,turnSmoothTime);
//transform.rotation = Quaternion.Euler(0f,angle,0f);
float rot = Input.GetAxis("Mouse X");
moveDirection = transform.TransformDirection(moveDirection);
rb.AddTorque(transform.up * rotSpeed * rot);
rb.velocity = new Vector3(moveDirection.x * moveSpeed, rb.velocity.y, moveDirection.z * moveSpeed);
//transform.rotation = Quaternion.Euler(0f,0,0f);
}
private void idle()
{
Debug.Log("Idle");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY ); //
}
private void walk() /////WALK
{
moveSpeed = walkSpeed;
anim.SetFloat("run", 0);
Debug.Log("walk");
float MMX = Input.GetAxis("Horizontal");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY);
anim.SetFloat("InputX",MMX);
}
private void run() ///// RUN
{
moveSpeed = runSpeed;
anim.SetFloat("run", 1);
float MMX = Input.GetAxis("Horizontal");
float MMY = Input.GetAxis("Vertical");
anim.SetFloat("InputY",MMY);
anim.SetFloat("InputX",MMX);
}
private void jump()
{
anim.SetFloat("JumpAnim", 1);
Debug.Log("jump");
//rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
rb.AddForce((Vector3.up) * jumpHeight, ForceMode.Impulse);
// rb.AddForce(0, 0, jumpHeight, ForceMode.Impulse);
//velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); //
}
}
Your answer
Follow this Question
Related Questions
Realistic Soccer Ball Dribbling? 1 Answer
stop the ball from moving after button is pressed 1 Answer
Everything that moves need an Rigid Body? 1 Answer
Rigidbody movement instead of transform? 0 Answers
Respawn objects 1 Answer