The question is answered, right answer was accepted
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");
}
@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");
}
}
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?
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
I think Vector2.up may be moving the object in the z coord rather than I, try doing
new Vector3 (0, 1, 0)
IconFigOne is a rigidbody2d. Have your tried IconFigOne.velocity = Vector2.up * speed
or IconFigOne.addForce ()
@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.
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)
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;
}
}
Follow this Question
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