Movement based on character relative to camera. (Updated)

RE-EDIT: Here’s a video of the kind of gameplay I’m talking about, since I suck at explaining it.

EDIT: Got a few things figure’t. Basically, I’m making the character move based on the direction the camera is moving. With the old script, this meant that if the camera looked from above, the character would attempt to move down into the terrain. It was based on a rigidbody-based FPS walker.

I took a section from Lerpz’ ThirdPersonControl script, and added it in.

With my new mashup of two scripts, it now goes in the proper direction, but the acceleration system of the old script isn’t working.

Old script: Acceleration working, direction not working.

function FixedUpdate ()
{
    if (grounded)
    {
        // Calculate how fast we should be moving
        var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		targetVelocity = camera.main.transform.TransformDirection(targetVelocity);
        targetVelocity *= speed;
       
        // Apply a force that attempts to reach our target velocity
        var velocity = rigidbody.velocity;
        var velocityChange = (targetVelocity - velocity);
        velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
        velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
        velocityChange.y = 0;
        rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);

   }

New script: Direction working derived from camera, but the forward variable somehow needs to match the character’s orientation. Acceleration not working. (Speed is absolutely constant.)

function FixedUpdate ()
{
    if (grounded)
    {
		// Forward vector relative to the camera along the x-z plane	
		var forward = Camera.main.transform.TransformDirection(Vector3.forward);
		forward.y = 0;
		forward = forward.normalized;

		// Right vector relative to the camera
		// Always orthogonal to the forward vector
		var right = Vector3(forward.z, 0, -forward.x);
		
        // Calculate how fast we should be moving
		var v = Input.GetAxisRaw("Vertical");
		var h = Input.GetAxisRaw("Horizontal");
		
		var targetVelocity = h * right + v * forward;
		targetVelocity = targetVelocity.normalized;
        targetVelocity *= speed;
       
        // Apply a force that attempts to reach our target velocity
        var velocity = rigidbody.velocity;
        var velocityChange = (targetVelocity - velocity);
        velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
        velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
        velocityChange.y = 0;
        rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
}

[/code]

Can you explain a bit further how the camera will move? Is it always positioned “above” the character?

It will follow the character, and usually trails behind it unless the player moves it manually. Most of the time, it will rotate on its Z axis so that its “up” direction roughly matches the player’s “up”.

I guess I could best explain it as, "Rotate the camera’s Z to the character’s Z rotation based on the camera’s local Z axis.

Similarly, with the controls, I want the movement direction to be based on “the character’s X and Z rotation based on the camera’s local X and Z axis”.

i know a really easy way to get that camera control.
the hard part is to make it change camera rotation directions based on what facing direction he is walking on aswell as controller (walls,cieling)
but my solution is not vectorBased camera movement though… if that was what you needed.

I’m not picky! :smile:

I’m not much of a scripter yet, so I don’t know many methods. I will be happy with anything that gets the job done.

i’m not sure if i understand exactly what you are looking for. are you supposed to move the camera around the character while he is moving? or do you just want to be able to rotate the camera around the player while he is standing still and as soon as you start walking, the character walks into the direction that the camera is looking?

With the right control stick (or whatever) the camera can be rotated around the character. Like a platformer for example, the left control stick moves the character around based on which direction the camera is facing.

The problem is that if the camera is moved above the character, the camera is looking straight down at the ground, and the character attempts to move into the ground.

EDIT: Made a big change to the opening post, now including that segment of my script. It seems my targetVelocity variable has gone from a proper vector to something else…