- Home /
Preventing Movement - Character Controller - 2D
Hello everybody,
So I want my character to NOT move when stumbling upon rigidObjects, I've attached character controller to my player and added a boulder Object.
I had my character have rigid body && box collider but I was told character controller might be more suitable when using the player.
But when I "collide" with objects it keeps moving, what am I missing?
The setup is as follows:
Player: has character controller attached and when it moves it moves as follows":
When up button is pressed:
transform.Translate (Vector3.up * speed * Time.deltaTime);
Boulder Object is as follows"
Rigidbody2D && box Collider2d attached....
Thanks for your time and help!
Answer by PouletFrit · May 30, 2014 at 05:10 PM
You shouldn't move gameobject that have a rigidbody or a character controller with translate. Translate will "teleport" your gameobject right to the position, and thus not detecting collision. Use rigidbody.MovePosition for gameobject with rigidbody or SimpleMove or Move with character controller.
public float speed = 3.0F;
public float rotateSpeed = 3.0F;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
}
Take a look at Character Controller Unity API
So Imagine I created a button in the HUD. $$anonymous$$y character ain't moving.
There's no "rotating" with my character.
You either move Y+ or Y-, X+ or X-....
Here's an example with your code of moving along the Y+ axis.
if (gui.upClicked == true) {
forward = transform.TransformDirection(Vector3.up);
Debug.Log (forward);
_controller.Simple$$anonymous$$ove(forward * speed * Time.deltaTime);
//Clamps the position, so if the user goes beyond $$anonymous$$ or max he won't move.
tempPosition = transform.position;
tempPosition.y = $$anonymous$$athf.Clamp (tempPosition.y, $$anonymous$$Y, maxY);
tempPosition.x = $$anonymous$$athf.Clamp (tempPosition.x, $$anonymous$$X, maxX);
transform.position = tempPosition;
gui.upClicked = false;
}
Well....the player ain't moving. What am I missing? Sorry
I think for Simple$$anonymous$$ove to work, your gameObject would need to be considered grounded, so I'm not sure that you should use it.
Depends on what kind of game you want to make, but I think that rigidbody2D and a Collider2D would be more appropriate in your case.
void OnGUI() {
if (GUI.Button(new Rect(10,10,50,20), "Up") {
rigidbody.$$anonymous$$ovePosition(Vector3.up * speed * Time.deltaTime);
tempPosition = transform.position;
tempPosition.y = $$anonymous$$athf.Clamp (tempPosition.y, $$anonymous$$Y, maxY);
tempPosition.x = $$anonymous$$athf.Clamp (tempPosition.x, $$anonymous$$X, maxX);
transform.position = tempPosition;
}
}
The camera is almost identical to that of Pokemon Gameboy. As I stated the game is 2D so Im using RigidBody2D....Simple move won't work :(
Your answer