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);
}
}
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);
}
}
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.
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)
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.