imput.GetAxes ("Horizontal") moving/flip side together, help!
Hello guys, so i stucked with my stick boy too long, i surrender trying to figure out alone, here is the problem:
when i compil, my stick boy only moves to the right side, when i press left, only the image switches the side but my stick boy still keep moving to the right. when i remove the the condition ''if'' transform.eulerangles (0,180) (or transform.localscale (-1,1)), he backs to normal meaning that when a press left he move to the left and right when i press right, but i want to flip his side and move together! please help me! look at my script below
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public GameObject player;
public float speed;
public float MinHeight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
float translation = Input.GetAxis ("Vertical") * speed;
player.transform.Translate (0, translation, 0);
if (player.transform.position.y < MinHeight) {
player.transform.position = new Vector2 (transform.position.x, MinHeight);
}
float diresq = Input.GetAxis ("Horizontal") * speed;
player.transform.Translate (diresq, 0, 0);
if (Input.GetAxis ("Horizontal") < (0)) {
player.transform.eulerAngles = new Vector2 (0,180);
}
if (Input.GetAxis ("Horizontal") > (0)) {
player.transform.eulerAngles = new Vector2 (0,0);
}
}
}
Answer by b1gry4n · Sep 03, 2016 at 06:29 AM
You only need to get the input axis one time and as long as you store it you do not need to keep checking for input.
void Update()
{
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
player.transform.Translate(horizontal * speed, vertical * speed, 0);
if (player.transform.position.y < MinHeight)
{
//not sure if youre trying to match the players transform to the transform this script is on... but i changed this
player.transform.position = new Vector2(player.transform.position.x, MinHeight);
}
if (horizontal < 0)
{
player.transform.eulerAngles = new Vector2(0, 180);
}else
if (horizontal > 0)
{
player.transform.eulerAngles = new Vector2(0, 0);
}
}
Hello thanks a lot for trying to help! i compil your script and still have the same problem, the stick boy that i creates keeps moving to the right no matter if is pressed left or right, only the images is rotationed to the left or to the right.
Answer by DaedalusEnix · Sep 03, 2016 at 07:47 PM
I finally solved the problem but i even no understand what i did, and sounds ugly
public class Player : MonoBehaviour {
public GameObject player;
public float speed;
public float MinHeight;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
player.transform.Translate (horizontal * speed, vertical * speed, 0);
if (player.transform.position.y < MinHeight)
{
//not sure if youre trying to match the players transform to the transform this script is on... but i changed this
player.transform.position = new Vector2(player.transform.position.x, MinHeight);
}
if (horizontal < 0)
{
player.transform.Translate (horizontal * speed*-2, vertical * speed );
player.transform.eulerAngles = new Vector2(0, 180);
}else
if (horizontal > 0)
{
player.transform.eulerAngles = new Vector2(0, 0);
}
as you can see i put a new statment >>>> player.transform.Translate (horizontal* speed*-2, vertical * speed, 0); i'm not satisfied with this solution cause is non sense in my opinion, when i remove all the condition about rotationing and let the getaxis play alone, i see no problem with the movimentation, press left he goes to the left, press right he goes to the right. that's it, still awaiting a Real explanation, thank you.
void Update()
{
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
player.transform.Translate($$anonymous$$athf.Abs(horizontal) * speed, vertical * speed, 0);
if (player.transform.position.y < $$anonymous$$inHeight)
{
player.transform.position = new Vector2(player.transform.position.x, $$anonymous$$inHeight);
}
if (horizontal < 0)
{
player.transform.eulerAngles = new Vector2(0, 180);
}else
if (horizontal > 0)
{
player.transform.eulerAngles = new Vector2(0, 0);
}
}
Answer by villevli · Sep 03, 2016 at 06:21 PM
transform.Translate
moves the object relative to its local axes so the rotation of the object affects the direction of the movement. You can use transform.Translate(x, y, z, Space.World)
to move in world space. Or in your script you can try to make the horizontal
value absolute with Mathf.Abs()
. (Only for the translation since you later check if the value is negative)
Answer by ArturoSR · Sep 03, 2016 at 09:13 PM
Hello there.
OK, this is the answer to your problem, it happens to me the first time using Unity, so, the problem is here, you don't have to rotate the player controller, you need to create this hierarchy first:
Player (Controller Component) even you can control the Animator directly from this.
Main Character, must be a child from above (The Controller has to have a Transform field on it) and none Controller < This is important.
So, where ever you move your Controller (Axis Horizontal), it has to move left or right, but when is about to turn around (0 to 180), you need to rotate the child (the Character model), this way you can achieve your goal, also the better way to achieve it it's using the Character Controller component attached to the root not to the 3d model.
The error happens because you turn around the main root, I know for sure this going to works, because I use it a lot, cheers.
Your answer
Follow this Question
Related Questions
Getting vector 3 to move relative to camera 0 Answers
How to make GetAxis double tap dash/ reset dash speed upon release GetKeyUp?? 2 Answers
Replace Input.GetAxis with Touch. Is there a way of converting this: 0 Answers
GetAxis Mouse X and Mouse Y returning high number values. 0 Answers
Input Keyboard GetAxis not working 0 Answers