- Home /
CharacterController.SimpleMove() doesn't work
Hello guys,
I am working on "click to move" script to my game and everything in script works except line of code with SimpleMove(). Unity writes no error, it just does not move, even it rotates correctly. And yes, CharacterController is attached to Player gameobject and is assigned in the inspector. Here is the script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class CharacterMovement : MonoBehaviour {
private Vector3 _targetPosition;
public float speed;
public float rotationSpeed;
public Camera mainCamera;
public CharacterController controller;
void Start () {
_targetPosition = transform.position;
speed = 2f;
rotationSpeed = 4f;
}
void Update () {
if(Input.GetKeyDown(KeyCode.Mouse1)) {
Ray _ray;
RaycastHit _hit;
_ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(_ray, out _hit, 20) && _hit.collider.tag != "Player") {
_targetPosition = _hit.point;
}
}
if(Vector3.Distance(transform.position, _targetPosition) < 0.1f) _targetPosition = transform.position;
Move();
}
private void Move() {
if(transform.position != _targetPosition) {
Quaternion lookPosition = Quaternion.LookRotation(_targetPosition - transform.position);
lookPosition.x = 0;
lookPosition.z = 0;
transform.rotation = Quaternion.Slerp(transform.rotation, lookPosition, rotationSpeed * Time.deltaTime);
controller.SimpleMove(transform.forward * speed);
}
}
}
No idea whats up.
Try a debug.log just before the call to simple move.
Try setting speed really high.
I setted speed to 1 000 and still Player is not moving. I added Debug.Log before and after Simple$$anonymous$$ove and both of them appeared in the console...Could this be a Unity bug or something?
It might be that your character is stuck in the ground or blocked by any invisible collider? Try moving it above the ground and to a new position.
Also one thing to note is that Simple$$anonymous$$ove() documentation page says that:
Gravity is automatically applied. Returns if the character is grounded.
So also check if your character is grounded properly.
Possible, but not likely. 99% of problems like this are caused by something else.
Have you tried putting the character in a new scene with just a flat plane?
Any scaling of things involved?
Answer by Magnomous · Jul 01, 2014 at 09:11 AM
Ok, I used a google and found very similar article:
http://answers.unity3d.com/questions/277402/controllersimplemove-having-issues-with-vector3for.html
The problem was, that my cube had a child object with collider touching the cube. When I checked child's object collider as Trigger, everything works correctly now. Thank you all for your responses.
Answer by unity_OGYwU_NIcUzFuA · May 27, 2020 at 10:01 AM
Sorry to dig up a very old post but this is the first one that comes when googling this issue. I just experienced troubles with SimpleMove myself and it took me all afternoon to figure out the problem, so I want to post the solution here for reference.
As it turned out, I was "breaking" my Character Controller by calling SimpleMove(somthing / 0f) at the very first frame. Even though I was calling it with reasonable values afterwards, it was completely unresponsive.
Since SimpleMove requires a speed, it's frequence to call it with a fraction of dist/time so be sure that you NEVER call it with a time of 0.
Your answer
Follow this Question
Related Questions
Run speed in world-based character controller 0 Answers
Multiple Cars not working 1 Answer
C# CharacterController.SimpleMove Goes on Forever 1 Answer
Distribute terrain in zones 3 Answers