- Home /
The controller.move does not work correctly
Hello, I have a problem in my code, I want to move my character with the CharacterController to a specific point on the ground. The problem is in: _Controller.Move (Vector3.Lerp (_PositionOrigin, _PositionDestination, speedMove * Time.deltaTime)); , the character does not go to the indicated point.
The transform.position = vector3.MoveTowars, just makes the movement as I want, but with that method you can not detect collisions.
What can I do to fix the movement with the CharacterController? and that moves just like the MoveTowars?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveInter : MonoBehaviour {
public Vector3 _PositionOrigin;
public Vector3 _PositionDestination;
private bool _IsActive;
private string taged;
public Animator _Animate;
public GameObject origin;
public CharacterController _Controller;
public Vector3 _PositionHelp;
private Vector3 lookAtTarget;
private Quaternion playerRoot;
public float speedRoot;
public float speedMove;
void Start () {
_IsActive = false;
_Animate = origin.GetComponent<Animator>();
_Controller = GetComponent<CharacterController>();
_PositionOrigin = transform.position;
}
void Update () {
_PositionHelp = transform.position;
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
_IsActive = true;
_PositionDestination = hit.point + (hit.normal * transform.localScale.y / 25.0f);
taged = hit.collider.gameObject.tag;
lookAtTarget = new Vector3(_PositionDestination.x - transform.position.x, transform.position.y, _PositionDestination.z - transform.position.z);
playerRoot = Quaternion.LookRotation(lookAtTarget);
}
}
if (_IsActive) {
if (taged == "Floor")
{
//transform.position = Vector3.MoveTowards(_PositionOrigin, _PositionDestination, speedMove * Time.deltaTime);
//transform.LookAt(_PositionDestination);
transform.rotation = Quaternion.Slerp(transform.rotation, playerRoot, speedRoot * Time.deltaTime);
_Controller.Move(Vector3.Lerp(_PositionOrigin, _PositionDestination, speedMove * Time.deltaTime));
}
else if (taged == "EnemyCh")
{
transform.rotation = Quaternion.Slerp(transform.rotation, playerRoot, speedRoot * Time.deltaTime);
_Controller.Move(Vector3.Lerp(_PositionOrigin, _PositionDestination, speedMove * Time.deltaTime));
}
else if(taged != "Floor" && taged != "EnemyCh")
{
_IsActive = false;
_PositionDestination = _PositionOrigin;
}
}
if(_PositionHelp==_PositionDestination)
{
_Animate.SetBool("Walk", false);
_Animate.SetBool("Atack", false);
}
else
{
_Animate.SetBool("Walk", true);
_Animate.SetBool("Atack", false);
}
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
Debug.Log("collsion");
if (hit.collider.gameObject.tag == "EnemyCh" || hit.collider.gameObject.tag == "Character")
{
_PositionHelp = _PositionOrigin;
Debug.Log("collsion");
_Animate.SetBool("Atack", true);
}
}
}
Your answer
Follow this Question
Related Questions
Changing the landing and switch directions of standard assets TPC controller 0 Answers
Making a bubble level (not a game but work tool) 1 Answer
Moving a CharacterController Object together with a RigidBody Object 1 Answer
Vector3.Movetoward. Why does this work? 0 Answers
How to get our character controller script to make our player move? 1 Answer