movement direction player relative to camera and lookat goes bad

Hi

I’ve got the following problem which I hope somebody can help me with.

Ive got the player movement relative to the camera, so the input axis stay the same regardless of the camera rotation. I also use a lookat function on the camera itself, so the player will always be followed. The horizontal movement isn’t horizontal to the world anymore, but relative to the camera (so bends off with the rotation of the camera). I know this is the way it should be, but is there a way to counter the rotation, so horizontal movement stays horizontal to the world AND input axes relative to the camera?

important part player movement script:

var forward = cameraTransform.TransformDirection(Vector3.forward);

	forward.y = 0;
	forward = forward.normalized;

<snip>

				moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
				moveDirection = moveDirection.normalized;

	var movement = moveDirection ;
	movement *= Time.deltaTime;
controller.Move(movement);

camera just uses the regular transform.LookAt(ThePlayer);

I use Uniscript

thanks alot,
cheers
Frank

I assume that you are using something like the MouseOrbit script or some such on your camera.

have a look at this:

function Update(){
	var camPos = Camera.main.transform;
	Camera.main.transform.position = transform.postion;
	Camera.main.transform.localEulerAngles.x = 0;
	
	
	var rotation = transform.rotation;
	transform.rotation = Camera.main.transform.rotation;
	
	Camera.main.transform.position = camPos;
	Camera.main.transform.LookAt(transform);
	
	transform.rotation = Quaternion.Lerp(rotation, transform.rotation, 2.0 * Time.deltaTime);
	
	var moveDirection = transform.forward;
	var move = moveDirection * Input.GetAxis("Vertical") * 5 * Time.deltaTime;
	
	controller.Move(move);
}

Thank you for your quick reply. I didnt mentioned I use my own script on the camera. Its parented to the player, with an offset. I wan the camera always to be to the side of the character, but part of the levels also turn, so I swap the offset (with help of triggers) to the other axis to keep it to the side of the player. here’s the full cam script:

Im not familiar with the mouse orbit script…

var CamHeight : float = 15;
var CamDistance : float = 10;
var ThePlayer : Transform;
var TheCamera : Transform;

private var velocity = Vector3.zero;
var smoothTime = 0.3;

static var Interesting : boolean = false;
static var InterestPosition : Vector3;
static var ChangeDirection : boolean = false;

static var MirroredZ : boolean = false;
static var MirroredX : boolean = false;
static var InterestSmoothTime : float = 1;

 
function Update () {


	TheCamera.transform.LookAt(ThePlayer);



var CamYoffset = ThePlayer.position.y + CamHeight;

if(!ChangeDirection  !MirroredX)
{
	CamXoffset = ThePlayer.position.x + CamDistance;
	CamZoffset = ThePlayer.position.z ;

}

if(ChangeDirection  !MirroredZ)
{

	CamXoffset = ThePlayer.position.x;
	CamZoffset = ThePlayer.position.z + CamDistance ;

}


if(ChangeDirection  MirroredZ)
{
	CamXoffset = ThePlayer.position.x;
	CamZoffset = ThePlayer.position.z - CamDistance ;

}


if(!ChangeDirection  MirroredX)
{
	CamXoffset = ThePlayer.position.x - CamDistance ;
	CamZoffset = ThePlayer.position.z;

}

if(ChangeDirection  MirroredX)
{
	CamXoffset = ThePlayer.position.x ;
	CamZoffset = ThePlayer.position.z - CamDistance ;

}

var CamPosition = Vector3(CamXoffset,CamYoffset,CamZoffset);


		if(!Interesting)
	{

	
	transform.position = Vector3.SmoothDamp(transform.position, CamPosition, velocity, smoothTime);
	}


	if(Interesting)
	{
	
	 transform.position = Vector3.SmoothDamp(transform.position, InterestPosition, velocity, InterestSmoothTime);
	
	}


}

In your example the camera takes the position of the player 1 on 1 (like a child of the player), but Im using smoothdamp to make the cam smooth follow, this is where the problem occurs, because when the player moves while the cam hasnt reached its end point (last position of the player) it starts rotating towards the player (due to the lookat), and because the controls are relative to the camera the movement of the player isn’t straight anymore…

I have a hard time explaining, I hope you understand…

should I substract the rotation of the cam from player movement direction again to compensate in some way?

I managed to fix it:

I have a camera group (empty game object) with 2 childs: the main camera, and another empty gameobject. I translate the group to follow the movement of the player, us a lookat on the camera, and use the empty gameobject local rotations (with help of transformdirection) for the movement of the character. Because I only rotate on Y the direction of the player will keep in a straight line, and when the player walks around the corner I just rotate that gameobject 90 degrees.

7 Years old, but you saved my life today :slight_smile: