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

Moving a particular sprite up and down on an axis regardless of rotation

Discussion in 'Scripting' started by neonwarge04, Feb 2, 2016.

  1. neonwarge04

    neonwarge04

    Joined:
    Jan 12, 2016
    Posts:
    16
    I am making a spike which pops a needle every t interval. I am able to make this to work. But when I try to rotate my game object, say, sideways leftwards, the popping of needle literally goes up and down not sideways as I expect. I need it to move up and down on an axis. Currently, in my game the game object is facing the green line (y-axis). I want my needle to go up and down on this axis.

    Here is what I currently have:

    Code (CSharp):
    1.     void Pop(float elapsed)
    2.     {
    3.         float step = elapsed * speed;
    4.         transform.position = Vector3.MoveTowards (transform.position, popped, step);
    5.         transform.
    6.     }
    7.  
    8.     void Unpop(float elapsed)
    9.     {
    10.         float step = elapsed * speed;
    11.         transform.position = Vector3.MoveTowards(transform.position, relax, step);
    12.     }
    13.  
    Here is how I initialize the target positions:


    Code (CSharp):
    1.     void Start ()
    2.     {
    3.         relax  = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
    4.         popped = new Vector3 (transform.position.x, transform.position.y + 0.8f, transform.position.z);
    5.  
    6.         StartCoroutine (PopNeedles ());
    7.     }
    8.  
    What I am expecting is that the needle to pop out leftwards, since I made it face leftwards (via rotation)! I can circumvent this by making an object and appropriate scripts but that is very stupid and rubbish.

    I want it all done in a single game object.

    Thanks!
     
  2. domkia

    domkia

    Joined:
    Aug 19, 2012
    Posts:
    99
    I think the problem is that you set your vectors relaxed and poped only once during start method. When you rotate your object update those vectors too.
     
  3. neonwarge04

    neonwarge04

    Joined:
    Jan 12, 2016
    Posts:
    16
    I believe I don't have to? I want it to move to a specific axis. To its local axes. I was able to make this to work by using
    this method transform.Translate. But it goes on and on without stopping.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    all of your code is working on global position/axis. Probably want to look at localPosition etc.
     
  5. neonwarge04

    neonwarge04

    Joined:
    Jan 12, 2016
    Posts:
    16
    Thanks, actually I gave up, it doesn't fit quite well into the game so I decided to ditch the idea. Thanks you very much! I look into it when I come back to that idea again. Maybe I am able to make this work next time.
     
  6. domkia

    domkia

    Joined:
    Aug 19, 2012
    Posts:
    99
    As I said just recalculate relax and popped when rotating.
    Firstly you can simplify those lines:
    relax = transform.localPosition;
    popped = relax + transform.up * 0.8f;
    And make sure they get updated, so put them in to your PopNeedles() coroutine.
    It works

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NeedleExample : MonoBehaviour
    5. {
    6.     float speed = 2f;
    7.     float waitTime = 0.5f;
    8.     Vector3 relax;
    9.     Vector3 popped;
    10.  
    11.     void Start()
    12.     {
    13.         StartCoroutine(PopNeedles());
    14.     }
    15.     void Pop()
    16.     {
    17.         transform.position = Vector3.MoveTowards(transform.position, popped, Time.deltaTime * speed);
    18.     }
    19.     void Unpop()
    20.     {
    21.         transform.position = Vector3.MoveTowards(transform.position, relax, Time.deltaTime * speed);
    22.     }
    23.     IEnumerator PopNeedles()
    24.     {
    25.         while(true)
    26.         {
    27.             relax = transform.localPosition;
    28.             popped = relax + transform.up * 0.8f;
    29.             Pop();
    30.             yield return new WaitForSeconds(0.5f);
    31.             Unpop();
    32.             yield return new WaitForSeconds(0.5f);
    33.         }
    34.     }
    35. }
    36.