Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Movement dampening help

Discussion in 'Scripting' started by Drespar, May 5, 2016.

  1. Drespar

    Drespar

    Joined:
    May 5, 2016
    Posts:
    1
    So I am experimenting with creating a click-to-move script for a simple cube. I can get it to move in the direction of where the cursor is on a plane, which is a good start. However, it maintains the same velocity indefinitely. I am trying to figure out a way to damp the movement so it slows down to a complete stop at where the cursor clicked.

    So far my code for the head is such:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof (HeadControl))]
    5. public class Head : MonoBehaviour {
    6.  
    7.     public float moveSpeed = 5;
    8.     public Vector3 headVector;
    9.  
    10.     HeadControl control;
    11.  
    12.     Camera viewCamera;
    13.  
    14.     void Awake(){
    15.         control = GetComponent<HeadControl> ();
    16.         viewCamera = Camera.main;
    17.     }
    18.  
    19.     void Update () {
    20.         // Movement input
    21.         Ray ray = viewCamera.ScreenPointToRay (Input.mousePosition);
    22.         Plane groundPlane = new Plane (Vector3.up, 0);
    23.         float rayDistance;
    24.  
    25.         if (groundPlane.Raycast (ray, out rayDistance)) {
    26.             Vector3 point = ray.GetPoint (rayDistance);
    27.             Vector3 destination = new Vector3 (point.x, 0 , point.z);
    28.             //print (destination);
    29.  
    30.             headVector = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
    31.             Vector3 moveInput = destination - headVector;
    32.             Vector3 moveVelocity = moveInput.normalized * moveSpeed;
    33.             if (Input.GetMouseButtonDown (0)) {
    34.                 control.Move (moveVelocity);
    35.            
    36.             }
    37.         }
    38.     }
    39. }


    And the HeadControl Class is written as such:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof(Rigidbody))]
    5. public class HeadControl : MonoBehaviour {
    6.  
    7.     Vector3 velocity;
    8.     Rigidbody myRigidbody;
    9.  
    10.     void Start (){
    11.         myRigidbody = GetComponent<Rigidbody> ();
    12.     }
    13.  
    14.     public void Move (Vector3 _velocity){
    15.         velocity = _velocity;
    16.     }
    17.  
    18.     void FixedUpdate(){
    19.         myRigidbody.MovePosition (myRigidbody.position + velocity * Time.fixedDeltaTime);
    20.     }
    21. }


    I have been looking into Vector3.SmoothDamp as well as Mathf.Lerp; but I am struggling to figure out how to convert them into a usable syntax for either.

    Any help, tips, and advice are very welcome.

    Thank you!

    P.S. I would also appreciate criticism on how to better format my code.
     
  2. dre38w

    dre38w

    Joined:
    Nov 18, 2009
    Posts:
    137
    It continues to move because your velocity is keeping the moveSpeed as its value.

    I've done something like this before, but I used a much simpler method.

    I used Vector3.MoveTowards or Lerp if you want more of a smooth in smooth out. I'm sure there is a more sophisticated way of doing this but I would do something like this:

    Put the if(groundPlane.Raycast...) inside the if(Input.GetMouseButton...) statement. You could probably even combine the two ifs with &&. Then, use the MoveTowards function using parameters (transform.position, destination, moveVelocity) instead of the control.Move line. Not sure exactly what headVector is doing, but if it's used to compensate for something might need to replace destination with that moveInput variable, but I don't think you need it.

    Hope that made sense.