Jump to content

  • Log In with Google      Sign In   
  • Create Account

[Unity] Click to move and collision detection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 AforcefulFiasco   Members   -  Reputation: 116

Like
0Likes
Like

Posted 20 May 2013 - 06:04 PM

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!



Sponsor:

#2 kburkhart84   Members   -  Reputation: 1776

Like
0Likes
Like

Posted 20 May 2013 - 06:33 PM

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.





#3 Baloniman   Members   -  Reputation: 126

Like
0Likes
Like

Posted 21 May 2013 - 12:37 PM

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!

 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS