Unity3d movement reverses

Started by
6 comments, last by Durakken 10 years, 1 month ago

The basic problem is that pressing forward, the player object moves forward until I rotate and then it starts moving in a different direction relative to the camera...

After I started typing this I realized that the character was rotating, but the camera wasn't keeping to the back of the character.

But that comes with a new problem... I don't know how to rotate the camera the way I want to. It has to rotate around an offset center based on the character.

Can someone tell me what how to do that so that it corrects this problem?

Here's the movement script, input is handled in another

And followed by what I currently have for the camera.


using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
	float WalkSpeed = 3.0f;
	float RunSpeed = 6.0f;
	float TurnSpeed = 3.0f;

	void Update() {
		CharacterController Move = GetComponent<CharacterController>();
		GetInput controller = gameObject.GetComponent<GetInput>();

		//transform.Rotate(0, controller.Turn * TurnSpeed, 0);
		Vector3 move = new Vector3(controller.Strafe, 0, controller.Forward);

		float Speed = WalkSpeed;
		Move.SimpleMove(move * Speed);
	}
}

using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour {
	float TurnSpeed = 3.0f;

	// Update is called once per frame
	void Update () {
		GetInput controller = GameObject.Find("Player").GetComponent<GetInput>();

		transform.Rotate(0, controller.Turn * TurnSpeed, 0);
	}
}
Advertisement

For the movement, you probably want it in FixedUpdate rather than Update, otherwise you are going to get some weird skipping going on.

You probably want something along these lines, although there are certainly many, many, many other ways of doing it.


function FixedUpdate() {
	if(!isControllable)
		Input.ResetInputAxes();	
	else{
		if (grounded) {
			moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);
			moveDirection *= rollSpeed;
			
			//Find rotation based upon axes if need to turn
			moveHorz = Input.GetAxis("Horizontal");
			if (moveHorz > 0) // right
				rotateDirection = new Vector3(0, 1, 0);
			else if (moveHorz < 0) //left
				rotateDirection = new Vector3(0, -1, 0);
			else // not turning
				rotateDirection = new Vector3 (0, 0, 0);
							
			//Jump Controls
			if (Input.GetButton ("Jump")) {
				moveDirection.y = jumpSpeed;
			}			
	... assorted specialized controls here ....
	
		// Apply gravity to end Jump, enable falling, and make sure he's touching the ground
		moveDirection.y -= gravity * Time.deltaTime; 
		
		// Move and rotate the controller
		var flags = controller.Move(moveDirection * Time.deltaTime);  
		controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed);
		grounded = ((flags & CollisionFlags.CollidedBelow) != 0 );
	}
} 

As for the follow camera, there are even more ways to do that then there are player controls.

I think you might have misunderstood my problem... It's not that I couldn't "rotate" It's that I can't revolve.

I don't want to draw it out so just imagine that i'm describing a fixed directional space...

When I'm facing forward and my camera is facing forward everything is fine.

The problem is that when I rotate the character, the character is facing say, left or right, but the camera is still facing forward OR if I rotate the camera, the camera it's facing the same direction BUT it's now parallel to the character's facing direction, but it is still in its same position relative to the character...

So if my character is facing forward my position should be (0, 1, -5), but when I'm facing "backward" it should be (0, 1, 5), pointing at the character, but instead it is still at (0, 1, -5) pointing away, because the camera isn't revolving with the rotation... I don't know how to calculate that revolution.

Thank you for the above script, but I don't need any of that... at least I don't think I do >.>

I continued to search after I posted and found nothing that I could use that I understood... but I'll continue searching.

- Make sure your character model pivot is correct so the Z axis is on the front of the char.

- Rotate him only on the Y axis, move him forward only on the Z axis.

- This dude converted the built in scripts for cameras do C#:

http://psychicparrotgames.com/blog/2013/06/06/unity-third-person-controller-and-third-person-camera-scripts-converted-to-c/

You should check the scripts out and tweek it how you like it.

But personaly I think the public parameters that they provide are usualy enough to achieve what I want most of the time.

Hope that helps.

Have that already. Didn't help ^.^

I feel stupid though... I described what was happening wrongly... and I don't know why it's happening.

It appears the camera is revolving but the character isn't...which is odd cuz the script is on the character.

I've tried just putting those scripts on the character/camera and they don't do the thing i want for some reason and so even if I modify them or pick the stuff out of them that I think works, it's just a random guess. And worse is that It appears it is the code that makes it work is in multiple parts so even if I guess right about the code that makes it work, it still won't work, cuz it's not complete.


I feel stupid though... I described what was happening wrongly... and I don't know why it's happening.

I think every experienced programmer has gone through that. It is tough. Not just difficult as far as development, but also can make you doubt your skills.

One option is to visit the Unity forums and store and look over the many existing scripts out there that do the job. You don't get the experience of doing it, but you would have something that works.

So let's assume you want to write your own.

First, I recommend you take a short break and get some emotional distance if possible.

Then, disconnect the affected components. If you have source control keep your old version around, otherwise archive the old version in a safe place.

Then, start over. Take things one small step at a time.

Start out by carefully writing down your goals and requirements. Write down the desired result, not the math.

Since it is a camera, I'd start with a simple scripted object that follows a track. This is your character you want to watch. You can even attach a default character controller making them move with WASD or whatever, just make something that moves.

Then put an empty follow-cam script. Don't attach it to a camera. Attach it to a small cube. Start with the empty MonoBehavior and a FixedUpdate, and attach it to the small cube. Run, it should do nothing.

Then, very slowly, feature by feature, add a piece at a time to make the small cube follow the character in a nice smooth way that meets your needs. Maybe you want to go physics based for the motion of the camera and use LookAt to orient it correctly, or maybe you have some other way you want it to work.

Take things in small steps. Save out a copy (preferably with version control) every time you get a step. If something doesn't work right you can back up a step to the previously saved file and try again.

When you aren't sure what is wrong and you aren't sure how to describe it, just take a deep breath, clean everything out, and methodically follow the established processes for software development. And when you really get deeply stuck, you can solve almost any problem by adding an abstraction, then filling in the abstraction's details later.

I just set it aside, because it isn't quite important to what I wanted to figure out how to do which...

Place a char capsule

Place a trigger

move the capsule to the trigger

Freeze character movement + Trigger a battle menu

Be able to scroll through the options from that menu

Next step is to construct the basic elements for random battles

On another note...

Every time I come back here and look at this post I get a new idea as to why something might be going wrong hehe

Ok, what I think what might be going on is that for some reason the character is moving along the world x and y coordinates rather than using the internal rotation, but I'm not sure... Dunno why that would be the case though. I'm pretty sure this has a lot more to do with my lack of familiarity with coding as I haven't done a lot of it recently and little unity C# overall.

edit: Could it be something to do with this line from your script code?


moveDirection = transform.TransformDirection(moveDirection);

/end edit

The reason I want to program it myself is so I know exactly what it is doing so I can edit thing where I need to.

Well turns out that my last guess was right...

Here's the super simple script that allows forward/backward/left/right/horizontal camera rotation... no jumping is in the game so no "grounded" is needed.


using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
	float WalkSpeed = 3.0f;
	float RunSpeed = 6.0f;
	float TurnSpeed = 3.0f;
	float Speed;

	void Update() {
		CharacterController Move = GetComponent<CharacterController>();
		GetInput controller = gameObject.GetComponent<GetInput>();
		WorldStatus WorldStatus = GameObject.Find("Player").GetComponent<WorldStatus>();

		if(WorldStatus.mode == "awake") {
			transform.Rotate(0, controller.Turn * TurnSpeed, 0);
			Vector3 move = transform.TransformDirection(new Vector3(controller.Strafe, 0, controller.Forward));

			if(controller.Run == 1) {
				Speed = RunSpeed;
			} else {
				Speed = WalkSpeed;
			}
			Move.SimpleMove(move * Speed);
		}
	}
}

Question... Do you guys know if it would be possible to get rid of the call for character controller since it is in GetInput...

Also also I have a button repeat timer thingy to slow down input so that it doesn't go instantly to the next option... what's a good way to do this or a good number?

My testing out script is like this....


void Update() {
	if(myTimer > 0){
		myTimer -= Time.deltaTime;
	} else {
		if up/down go to next position

		myTimer = 5f * Time.deltaTime;
	}
}

It works... but not quite nicely. Sometimes it goes too fast and other times too slow...

This topic is closed to new replies.

Advertisement