Hey all! I'm working on a project that uses a click to move script to control the player. I'm having a small problem though, and it's hung me up for a good 3 hours. The character is just a cube, and he's walking on a plane. The problem comes into collision. I have a big cube in the middle of the plane that i want to run in to. Whenever you go to run at the cube, as soon as you hit the cube the player just starts to jitter around.
Here's the script for the walking:
using UnityEngine;
using System.Collections;
public class CTM : MonoBehaviour {
private Vector3 targetPosition;
public Vector3 dir;
private float dist;
private float move;
public float moveSpeed = 100f;
public bool isRunning = false;
public float stamina = 10;
public GameObject collCheck;
public GameObject check;
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown("w"))
{
if (isRunning)
isRunning = false;
else if (!isRunning && stamina >= 0)
isRunning = true;
}
if (isRunning == false)
{
moveSpeed = 20f;
if (stamina < 10)
stamina += Time.deltaTime;
}
if (isRunning == true)
{
moveSpeed = 30f;
stamina -= Time.deltaTime;
if (stamina <= 0)
isRunning = false;
}
//Revised code from JS ClickToMove
if(Input.GetKey(KeyCode.Mouse1))
{
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist))
{
Vector3 targetPoint = ray.GetPoint(hitdist);
//targetPosition = ray.GetPoint(hitdist);
//var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.LookRotation(targetPoint - transform.position);
}
//if (rigidbody.for
//rigidbody.AddRelativeForce(new Vector3(0,0,1) * moveSpeed , ForceMode.Force);
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
dir = targetPosition - transform.position;
dist = dir.magnitude;
move = moveSpeed * Time.deltaTime;
/*
Destroy(check);
check = GameObject.Instantiate(collCheck, transform.position + (targetPosition - transform.position).normalized * moveSpeed * Time.deltaTime, Quaternion.identity) as GameObject;
if (check.GetComponent<CollisionCheck>().collided == true)
dir = Vector3.zero;
*/
if(dist > move)
{
//transform.position += dir.normalized * move;
}
else
{
//transform.position = targetPosition;
}
//transform.position += (targetPosition - transform.position).normalized * moveSpeed * Time.deltaTime;
}
}
I can provide more info if needed. Also will give skype ID if you think you can help over voice chat! Thanks!