• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By RoKabium Games
      While looking out for that pesky Terrator, our little alien is doing a bit of relaxed mining down on the new gas planet "Lelantos" this weekend.... 
      #gamedev #indiedev #madewithunity #screenshotsaturday
    • By vividgamer
      I have a native iOS game (objective c, XCode build) which I am considering to port to other platforms.
      Core gameplay is based on solely on geographical maps, and custom drawing over maps. It also has Core Data. This part is complete in development.
      What is not done yet is: monetization, gamification (leaderboards, challenges) and multiplayer functionality.
      As I think more about it, I am tempted to think if this is the right time to move to a cross platform tool such as Unity. But before dedicating time to port my 5 years side-project effort in Objective C, I really want to know if its worth it.
      - Does Unity support such plugins / assets that will fulfill all my above requirements?
      - Unity Personal seems to have only 20 concurrent users - is it too costly scaling if I decide for extending to web and android platforms?
      - What is the general workflow involved in publishing to iOS, Android, PC, and web platforms while using Unity? I mean to ask about various points of signing stuff, paying fees and getting certified.
      - How long will it really take to port my entire Objective C project into Unity? I am somewhat familiar with C# but I am finding it hard fidgeting with Unity IDE as lot of things are focused around FPS and 3D while my game is still 2d - not much action involved. I seem bit overwhelmed by the list of features I see there. All in all, I do not want to lose my momentum while still making sure its portable to everywhere.
      - Any assets I could use (for free to try basis in debug) that are relevant for my game?
      - Last but not the least, are there any costs that I need to be paying upfront to Unity, for using it (apart from their monthly subscription model)? I don't understand their costing for multiplayer in conjunction with their subscription fees - if someone could kindly elaborate.
      Thanks in advance for your time reading a newbie
    • By GytisDev
      me and few friends are developing simple city building game with unity for a school project, think something like Banished but much simpler. I was tasked to create the path-finding for the game so I mostly followed this tutorial series up to episode 5. Then we created simple working system for cutting trees. The problem is that the path-finding is working like 90% of the time, then it get stuck randomly then there's clearly a way to the objective (tree). I tried looking for some pattern when it happens but can't find anything. So basically I need any tips for how I should approach this problem.
      Use this image to visualize the problem.
    • By aymen
      please any know how can i' calculate the centroid from any number vertices
    • By Henry Fernandez
      Good day sir/maam. I am developing a game for my thesis and im done with multiplayer and plan to start the implementation of AI but i dont know how/where to start. Please give an advice. I am developing it in C# using UNITY. 
      Im am now collected all pieces that has possible moves but i am stuck on which best move to select. I hope you will help me. This is link explained the game https://en.wikipedia.org/wiki/Game_of_the_Generals 

  • Advertisement
  • Advertisement
Sign in to follow this  

Unity [Unity] Click to move and collision detection

This topic is 1761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
            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;
        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;
            //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

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

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






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)





Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement