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

Smooth Ending (camera position)

Discussion in 'Scripting' started by Orolab, Jun 10, 2014.

  1. Orolab

    Orolab

    Joined:
    May 22, 2014
    Posts:
    3
    Hello,

    first time post, could really use your help!

    i want to add smooth in the end when i move the camera position with mouse.

    here is my code

    ----------------------------------------------------------------------------------------------------------------------------------
    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.         {
    3.             oldInputPosition = Input.mousePosition;
    4.         }
    5.         if (Input.GetMouseButton(0))
    6.         {
    7.             float xDif = Input.mousePosition.x - oldInputPosition.x;
    8.             if(!naturalMotion){xDif *= -1;}
    9.             if(xDif != 0){
    10.                camParent.transform.position += Vector3.right * xDif * tumbleSensitivity * Time.deltaTime;}
    11.             oldInputPosition = Input.mousePosition;
    12.         }
    13.         if (Input.GetMouseButtonUp (0))
    14.         {
    15.             oldInputPosition = Input.mousePosition;
    16.         }
    -----------------------------------------------------------------------------------------------------------------------------------
    i add vector3.lerp here, but i know that there is sometings missing
    Code (CSharp):
    1. transform.position = Vector3.Lerp(transform.position, xDif, tumbleSensitivity * Time.deltaTime);

    thanks a lot
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    How are you converting screen to the 3d world?

    http://docs.unity3d.com/ScriptReference/Camera.html
    http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
    http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

    Once you get a position on the screen, or wherever you are moving things too, you will need to use that position instead of the mouse position.

    Look into converting that ScreenPointToRay to get a point using Physics or a Plane. This will give you a good reference point in the world for where you want to move to.

    The Lerp, will do the smoothing for you, but it will never reach the exact point. (if that matters to you) If it matters, look at:

    http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
     
  3. Orolab

    Orolab

    Joined:
    May 22, 2014
    Posts:
    3
    thank you very much bigmisterb!

    i'm afraid that i didn't exactly understand, i'm new on unity exuse me ^^
    so i have to convert ScreenPointToRay but i don't know how.
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    erm... click on the link I provided. ;)
     
  5. Orolab

    Orolab

    Joined:
    May 22, 2014
    Posts:
    3
    @bigmisterb
    i didn't succeed with the ScreenPointToRay link :(
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Here... with some annotations.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PointAndMove : MonoBehaviour {
    6.    public float speed = 5;
    7.    private Vector3 target;
    8.    
    9.    void Start(){
    10.      target = transform.position;
    11.    }
    12.    
    13.    void Update(){
    14.      if (Input.GetMouseButtonDown(0)){
    15.        // make a plane from this object
    16.        Plane plane = new Plane(transform.up, transform.position);
    17.        float dist;
    18.        
    19.        // collect the ray from the screen where the mouse is.
    20.        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    21.        
    22.        // test to see if the click was actually on the plane.
    23.        if(plane.Raycast(ray, out dist)){
    24.          // set the new target from the mouse click.
    25.          target = ray.GetPoint(dist);
    26.        }
    27.      }
    28.      
    29.      // move the object if needed
    30.      if(transform.position != target){
    31.        transform.LookAt(target);
    32.        transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    33.      }
    34.    }
    35. }
    36.