I’ve been trying to make this work for a few hours and I just can’t figure it out.
I wrote out a script myself, but as soon as I stopped the character would go back to facing the first direction it was in.
I googled a solution, got another script that was supposed to fix this, but it has the exact same problems. Same happened the third time I tried.
Am I missing something here? Why does my player keep going back to its default position?
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class Controls : MonoBehaviour {
public float speed;
private Rigidbody rb;
Vector3 movement;
public float moveHorizontal;
public float moveVertical;
public Vector3 jump;
public float jumpForce;
public bool isGrounded = true;
private Vector3 curLoc;
private Vector3 prevLoc;
private float curLocX;
private float prevLocX;
private float curLocZ;
private float prevLocZ;
void Awake(){
rb = GetComponent<Rigidbody> ();
jump = new Vector3(0.0f, 2.0f, 0.0f);
}
void OnCollisionStay() {
isGrounded = true;
}
void Update () {
ControllPlayer();
}
void ControllPlayer()
{
float moveHorizontal = Input.GetAxisRaw ("Horizontal");
float moveVertical = Input.GetAxisRaw ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
if (transform.position != prevLoc) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement), 0.15F);
}
transform.Translate (movement * speed * Time.deltaTime, Space.World);
// if (Input.GetKey (KeyCode.Space) && isGrounded == true) {
// rb.AddForce (jump * jumpForce, ForceMode.Impulse);
// isGrounded = false;
// }
}
private void InputListen() {
Debug.Log (transform.position.x);
prevLoc = curLoc;
curLoc = transform.position;
prevLocX = curLocX;
curLocX = transform.position.x;
prevLocZ = curLocZ;
curLocZ = transform.position.z;
}
}