Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Mar 14, 2016 at 01:00 PM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnightRiderGuy · Mar 12, 2016 at 04:31 PM · c#movementspritestransform.positionanimated

Why Can't I get 2D object to Move?

I have this 2D animated sprite I am trying to get to move from another script but for some reason only the animation will play but the sprite does not move?

 //UP Movement Direction Control
     public void COne_DirectionUP(){
         Fig01Ani = FigureOne.GetComponent<Animator>();
         IconFigOne = GameObject.Find("Figure01Ani").GetComponent<Rigidbody2D>();
         IconFigOne.transform.Translate (Vector2.up * speed);
 
         //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
         //SFXfw.FigWalkSFX ();
         Fig01Ani.Play ("Fig01MoveAni");
     }
Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fydar · Mar 12, 2016 at 04:58 PM 0
Share

Is the "speed" variable set to 0?

avatar image KnightRiderGuy Fydar · Mar 12, 2016 at 05:44 PM 0
Share

@Fydar, nope.... I don't get it either I can call the object to move with the button inputs but trying to call the object to move via the "public void COne_DirectionUP()" I get nothing except the animation playing, it very frustrating why one method will work an not the other.

I'll post the whole code just in case I'm missing something. I had to pull a few of the buttons out in order to get the relevant stuff into this reply.

 using UnityEngine;
 using System.Collections;
 
 public class Character01Input : $$anonymous$$onoBehaviour {
     
     //Reference to the Character 01 Game Object
     public GameObject FigureOne;
 
     //animator reference
     private Animator Fig01Ani;
 
 
     public float speed;
     public Rigidbody2D IconFigOne;
 
 
 
 
     void Start(){
         Fig01Ani = FigureOne.GetComponent<Animator>();
         IconFigOne = GetComponent<Rigidbody2D> ();
         //disable it on start to stop it from playing the default animation
         Fig01Ani.enabled = true;
     }
 
 
 
     void Update () {
 
 
         
 
         
 
         
 
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S)) {//start UP $$anonymous$$ovement
             //Fig01Ani.enabled = true;
             transform.Translate (Vector2.up * speed);
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01$$anonymous$$oveAni");
         }
 
         else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.S)) {//Stop
             //Fig01Ani.enabled = true;
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01IdleAni");
         }
 
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W)) {//Start DOWN $$anonymous$$ovement
             //Fig01Ani.enabled = true;
             transform.Translate (-Vector2.up * speed);
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01$$anonymous$$oveAni");
         }
 
         else if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.W)) {//Stop
             //Fig01Ani.enabled = true;
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01IdleAni");
         }
 
     }
 
 
     //UP $$anonymous$$ovement Direction Control
     public void COne_DirectionUP(){
         Fig01Ani = FigureOne.GetComponent<Animator>();
         IconFigOne = GameObject.Find("Figure01Ani").GetComponent<Rigidbody2D>();
         IconFigOne.transform.Translate (Vector2.up * speed);
 
         //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
         //SFXfw.FigWalkSFX ();
         Fig01Ani.Play ("Fig01$$anonymous$$oveAni");
 
     }
 
 
 }
 
avatar image KnightRiderGuy Fydar · Mar 12, 2016 at 06:37 PM 0
Share

I tried putting a Debug in there and it's definitely reaching that part of the blasted code but why it won't move is driving me nuts?

avatar image Ali-hatem KnightRiderGuy · Mar 12, 2016 at 08:21 PM 0
Share

transform.Translate(Vector3.up * speed); & if you use transform.Translate you don't need Rigidbody2D which you already not using it in your script !!! Rigidbody2D should have property of velocity to move

Show more comments
avatar image Fydar Fydar · Mar 12, 2016 at 07:58 PM 0
Share

I think Vector2.up may be moving the object in the z coord rather than I, try doing

new Vector3 (0, 1, 0)

avatar image LazyElephant · Mar 12, 2016 at 07:29 PM 0
Share

IconFigOne is a rigidbody2d. Have your tried IconFigOne.velocity = Vector2.up * speed or IconFigOne.addForce ()

avatar image KnightRiderGuy LazyElephant · Mar 12, 2016 at 09:13 PM 0
Share

@LazyElephant I did try the first one but not the addForce one. I think it was because it was not being called inside of update? I found a solution using bools to check if my desired moment was true or false and then I call my transform.Translate inside of the void update, that seems to work very well.

avatar image Orolo · Mar 12, 2016 at 08:14 PM 0
Share

Why the line

 IconFigOne = GameObject.Find("Figure01Ani").GetComponent<Rigidbody2D>();

?

would it work if you commented that line out?

also, how are you calling COne_DirectionUP? do you call it per frame, or just once? if you just call it once, are you sure it doesn't just move up a little bit and then stops because you stopped calling it? (i ask because the upward movement in your update that you said works would be called every frame)

avatar image KnightRiderGuy Orolo · Mar 12, 2016 at 09:13 PM 0
Share

@Orolo, $$anonymous$$ostly experimenting ;)

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by KnightRiderGuy · Mar 12, 2016 at 08:09 PM

Thanks @Fydar I figured it out as it turns out that blasted "transform.Translate" seemed to ONLY want to work inside of update so I had to get a little crafty with some bools to check if my desired direction was true or false and then call my direction moment inside of the update.... not sure if there was a more appropriate way to do this but this seems to work.

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class Character01Input : MonoBehaviour {
 
 
 
     //Reference to the Character 01 Game Object
     public GameObject FigureOne;
 
     //animator reference
     private Animator Fig01Ani;
 
 
     public float speed;
     public Rigidbody2D IconFigOne;
 
     //Direction Bools
     public bool dirUp = false;        //UP
     public bool dirDown = false;    //Down
     public bool dirLeft = false;    //Left
     public bool dirRight = false;    //Right
     public bool stopMove = false;    //Stop
 
 
 
 
 
     void Start(){
         Fig01Ani = FigureOne.GetComponent<Animator>();
         IconFigOne = GetComponent<Rigidbody2D> ();
         //disable it on start to stop it from playing the default animation
         Fig01Ani.enabled = false;
     }
 
 
 
     void Update () {
         if (stopMove == true) {//Stop
             Fig01Ani.enabled = true;
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01IdleAni");
         }
 
         if (dirRight == true) {//Start RIGHT Movement
             Fig01Ani.enabled = true;
             transform.Translate (Vector2.right * speed);
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01MoveAni");
         }
 
         if (dirLeft == true) {//Start LEFT Movement
             Fig01Ani.enabled = true;
             transform.Translate (-Vector2.right * speed);
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01MoveAni");
         }
 
         if (dirUp == true) {//start UP Movement
             Fig01Ani.enabled = true;
             transform.Translate (Vector2.up * speed);
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01MoveAni");
         }
 
         if (dirDown == true) {//Start DOWN Movement
             Fig01Ani.enabled = true;
             transform.Translate (-Vector2.up * speed);
 
             //SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
             //SFXfw.FigWalkSFX ();
             Fig01Ani.Play ("Fig01MoveAni");
         }
 
     }
 
 
     //UP Movement Direction Control
     public void COne_DirectionUP(){
         dirUp = true;
         dirDown = false;
         dirLeft = false;
         dirRight = false;
         stopMove = false;
     }
 
     
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object move in the direction another object is facing on 2 axis? 1 Answer

transform.position is not updating the object's position 1 Answer

Need help with limited movement with mathf clamp 2 Answers

Rigidbody movement: Changing the Y of transform.position makes my character stutter 0 Answers

Sprite is disappearing in 2D game. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges