- Home /
What is the cause of the distorted movement of my character?
I've been working on making a movement script for a 3D character (in space but only moving along the X/Z plane for now - Left right, forward back etc), the character moves independently of the camera (so the camera stays where it is/whatever position the player put it in, whilst the character rotates). At present, the camera isn't my focus, I currently use a modified version of the 'MouseOrbit' script for that, but will modify further to what I need.
The issue I have, well theres two, is;
Mainly, my character will only rotate 180 degrees. And its only in the negative direction (I have to press 'A', pressing 'D' will do nothing until I have first rotated in the negative X direction). I've not been able to figure out why. The script (below) is placed in the 'Player' prefab with the actual mesh model and camera is within it also (the 'Player' reference/prefab only has the position, character controller and the mentioned script);
The other issue I have is the movement is skewed noticably. When moving with a rotation of 0degrees (IE the direction you face when you spawn in) its fine. However if I rotate 90 degrees to the right, I find myself moving to the left (relatively - IE as if I was reversing at 0 degrees rotation). I had this problem before, and I was unable to fix it.
Below is the script I use for my movement. As movement is meant to be independent of the camera, any references to the camera position/rotation etc do nothing, I just haven't removed them yet.
Any help would be greatly appreciated
using UnityEngine;
using System.Collections;
public class ShipControlScript : MonoBehaviour {
private Vector3 position;
private char userInput;
public Vector3 moveDirection;
public Vector3 targetDirection;
public Vector3 newHeading;
private bool rightClicked = false;
private CollisionFlags collisionFlags;
public Vector3 movement;
public float moveSpeed = 0.0f;
private float speedSmoothing = 10.0f;
public float targetSpeed = 0.0f;
private float impulse1Speed = 1.0f;
private float impulse2Speed = 2.0f;
private float impulse3Speed = 4.0f;
private float impulse4Speed = 8.0f;
private float rotateZ = 0.0f;
private float rotateY = 0.0f;
private string _CharacterState;
public CharacterController character;
public float rotateSpeed = 500.0f;
// Use this for initialization
void Start ()
{
position = character.transform.position;
moveDirection = transform.TransformDirection (Vector3.forward);
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (1))
rightClicked = true;
if (Input.GetMouseButtonUp (1))
rightClicked = false;
UpdateSmoothedMovementDirection ();
movement = (moveDirection * moveSpeed);
movement *= 1;
//movement *= Time.deltaTime;
transform.Translate (movement * Time.deltaTime);
//print (character.transform.position);
print("" + movement);
transform.rotation = Quaternion.LookRotation(moveDirection);
}
void UpdateSmoothedMovementDirection()
{
Transform cameraTransform = Camera.main.transform;
Vector3 forward = cameraTransform.TransformDirection (Vector3.forward);
forward.y = 0;
forward = forward.normalized;
Vector3 right = cameraTransform.TransformDirection (forward.z, 0.0f, -forward.x);
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
rotateZ = (v * rotateSpeed * Mathf.Deg2Rad * Time.deltaTime) / 360;
rotateY = (h * rotateSpeed * Mathf.Deg2Rad * Time.deltaTime) / 360;
//newHeading.z = rotateZ;
newHeading.x = rotateY;
if (newHeading.z <= -360)
newHeading.z = 0;
float curSmooth = speedSmoothing * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Alpha1) || Input.GetKeyDown (KeyCode.Keypad1)) {
_CharacterState = "Impulse 1";
targetSpeed = impulse1Speed;
}
else if (Input.GetKeyDown (KeyCode.Alpha2) || Input.GetKeyDown (KeyCode.Keypad2))
{
_CharacterState = "Impulse 2";
targetSpeed = impulse2Speed;
}
else if (Input.GetKeyDown (KeyCode.Alpha3) || Input.GetKeyDown (KeyCode.Keypad3))
{
_CharacterState = "Impulse 3";
targetSpeed = impulse3Speed;
}
else if (Input.GetKeyDown (KeyCode.Alpha4) || Input.GetKeyDown (KeyCode.Keypad4))
{
_CharacterState = "Impulse 4";
targetSpeed = impulse4Speed;
}
/*
if(_CharacterState == "Impulse 1")
targetSpeed = impulse1Speed;
else if(_CharacterState == "Impulse 2")
targetSpeed = impulse2Speed;
else if(_CharacterState == "Impulse 3")
targetSpeed = impulse3Speed;
else if(_CharacterState == "Impulse 4")
targetSpeed = impulse4Speed;
*/
targetDirection = -newHeading;// + forward;
if (rightClicked)
{
}
else
//targetDirection = moveDirection;
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
if(newHeading != Vector3.zero)
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
}
}