Question by
SylvesterAHansen · Sep 25, 2015 at 02:06 PM ·
c#movementcharactercharactercontroller
MoveTowards is curving for no reason
Hi there.
So I have a really weird issue.
I have a character controller, which is, mostly, controlled by a move toward function. The basic idea is that the player points to a point on the screen and then the player moves there. Which is what happens.
However, there is a major issue.
If the player walks into a wall for too long (or sometime just because my computer is being temperamental) the Character will move along a curve rather than a straight line. This is super annoying and seems to be causing some computers to crash while playing my game.
This is my code:
public class charCtrl : MonoBehaviour {
float distance = 50f;
public Vector3 target;
public float speed;
Vector3 HitPoint;
public GameObject character;
Animator anim;
bool walking;
public GameObject ZoomTarget;
public float targetDistance;
public GameObject hitsObject;
public Collider[] overlapObject;
public GameObject IntObject;
public bool Puzzle;
Rigidbody Rigi;
Vector3 lookatThat;
// Use this for initialization
void Start () {
//target = new Vector3 (0f, 0f, 0f);
anim = character.GetComponent<Animator>();
Rigi = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0) && !Puzzle) {
Ray rayOrigin = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast (rayOrigin, out hitInfo, distance, 1 << LayerMask.NameToLayer ("floor"))) {
HitPoint = hitInfo.point;
hitsObject = hitInfo.collider.gameObject;
}
target = new Vector3 (HitPoint.x, 0f, HitPoint.z);
character.transform.LookAt (target);
anim.SetFloat ("walk", 1);
walking = true;
overlapObject = Physics.OverlapSphere (target, 1, 1 << LayerMask.NameToLayer ("interactible"));
Rigi.velocity = new Vector3 (0, 0, 0);
}
transform.position = new Vector3 (transform.position.x, 0f, transform.position.z);
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
if (overlapObject.Length > 0)
IntObject = overlapObject [0].gameObject;
Quaternion poot = Quaternion.Euler (0f, 0f, 0f);
transform.rotation = poot;
if(walking){
Vector3 toot = new Vector3 (0f, 0f, 0f);
character.transform.localPosition = toot;
}
if(transform.position == target){
anim.SetFloat ("walk",0);
walking = false;
}
}
public void CollStopper(){
target = transform.position;
}
}
I really need help!!!
Comment