Sign in to follow this  
AforcefulFiasco

Unity [Unity] Click to move and collision detection

Recommended Posts

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!

Share this post


Link to post
Share on other sites
kburkhart84    3182

I didn't look in too much detail, but do you have anything that makes you stop once you collide with the big cube.  Physics(or simple collision response) is keeping the small cube outside of the big one, but that won't automatically stop the other script(Click to Move) from trying to progress towards its target.  So the jittering would be a constant collision, push out of collision, collision again....  You need to somehow stop the Click to move script upon collision, or it needs to be redone to go around collisions, depending on what you want.

Share this post


Link to post
Share on other sites
Baloniman    126

Make sure you are using a rigid body attached to your cube. In your collision code block set the rigidbody velocity to 0.

 

Example:

_________________________

 

 

var getRigid : RigidBody

 

 

 

OnCollisionEnter(getCollider : Collision){

 

getRigid.velocity.x = 0;

getRigid.velocity.y = 0;

getRigid.velocity.z = 0;

 

}

 

Should fix the problem. Also that code is from the top of my head you'll need to make a reference for your rigidbody for the example I used (getRigid)

 

 

Cheers!

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this