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

Vector2.SmoothDamp

Discussion in 'Scripting' started by AtomicCabbage33, Mar 24, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I want to smooth out the backgrounds movement as currently when I tap 'D' in quick succession the background almost lerps fowards which doesn't look very good. It is only when you hold the key down that the movement is smooth. I tried changing line 17 to 'Vector2 offset = new Vector2.SmoothDamp( Time.time * speed, 0);' but got the error 'The type name 'SmoothDamp' does not exist in the type 'Vector2' . Any help is appreciated :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BackGround_scroll : MonoBehaviour {
    5.  
    6.     public float speed = 0.5f;
    7.  
    8.     void Start ()
    9.     {
    10.    
    11.     }
    12.    
    13.     void Update ()
    14.     {
    15.         if (Input.GetKey(KeyCode.D))
    16.         {
    17.             Vector2 offset = new Vector2.SmoothDamp( Time.time * speed, 0);
    18.  
    19.             GetComponent<Renderer>().material.mainTextureOffset = offset;
    20.         }
    21.  
    22.     }
    23. }
    24.  
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    What you want to do actually?

    So you want to animate the texture offset, what kind of effect are you expecting?
     
  5. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    To create a parallax scroll effect. So as the player runs. the background moves with him. I have this working its just when the user only taps the 'a' or 'd' keys the background lerps fowards, rather than a smooth motion
     
  6. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Like in a 2D scroller. Then the scrolling should be the negative of the camera position but slower;

    Code (CSharp):
    1. var pos = Camera.main.transform.position;
    2. Vector2 pos2d = new Vector(pos.x, pos.y);
    3. float scrollSpeed = 0.1f;
    4.  
    5. GetComponent<Renderer>().material.mainTextureOffset = -pos2d*scrollSpeed;