Alright so i am trying to write a movement script kind of like what would be used for a 2d side scroller. my problem lies in having the character rotate to the direction he is walking. i cant seem to figure out a solution to make this work properly. here is the current code i have atm
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float speed = 10.0f;
public float rotationspeed = 100.0f;
private float moveBack = -1.0f;
private float moveForward = 1.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.A))
{
float translation = moveBack * speed;
//float rotation = moveBack * rotationspeed;
translation *= Time.deltaTime;
//rotation *= Time.deltaTime;
transform.position += Vector3.right * translation;
}
if(Input.GetKey(KeyCode.D))
{
float translation = moveForward * speed;
//float rotation = moveBack * rotationspeed;
translation *= Time.deltaTime;
//rotation *= Time.deltaTime;
transform.position += Vector3.right * translation;
}
}
}
I presume the problem is that the player keeps rotating forever? Also you might want to use Vector3.forward as the move direction, assuming the player character faces the walk direction. In case my assumption was correct, you may want to try a Mathf.Clamp method to constraint the rotation.
– asafsitner