0
votes

I have a character in Unity which use a move script, to transform its position.x, y, or z everytime when I press the appropriate button. Now I would like to change my code to just rotate the character when the user press the "a" or "d" and don't move it, however if the user press the "w" button move it forward. I have already looked for it on the internet, and found out that I would need to use Vector3.forward somehow, but it doesn't work.

Here is my script:

This is the script which actually moves the character: (It is attached to a Player object which contains the moveable characters)

public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;

Vector3 startPos;
Vector3 endPos;

bool firstInput;
public bool justJump;

// Update is called once per frame
void Update () {
    if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
        if (perc == 1) {
            lerpTime = 1;
            currentLerpTime = 0;
            firstInput = true;
            justJump = true;
        }
    }
    startPos = gameObject.transform.position;

    if (Input.GetButtonDown("right") && gameObject.transform.position == endPos) {
        endPos = new Vector3(transform.position.x + 0.5f, transform.position.y, transform.position.z);
    }
    if (Input.GetButtonDown("left") && gameObject.transform.position == endPos) {
        endPos = new Vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z);
    }
    if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
        endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 0.5f);
    }
    if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
        endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 0.5f);
    }

    if (firstInput == true) {
        currentLerpTime += Time.deltaTime * 5;
        perc = currentLerpTime / lerpTime;
        gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
        if (perc > 0.8f)  {
            perc = 1;
        }

        if (Mathf.Round(perc) == 1) {
            justJump = false;
        }
    }
}

}

And here is my "rotate script", which is attached to the moveable character(s) itself:

public class AnimationController : MonoBehaviour {

    Animator anim;
    public GameObject thePlayer;

    // Use this for initialization
    void Start () {
        anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {
        Move moveScript = thePlayer.GetComponent<Move>();
        if (moveScript.justJump == true) {
            anim.SetBool("Jump", true);
        }
        else {
            anim.SetBool("Jump", false);
        }

        if (Input.GetButtonDown("right")) {
            gameObject.transform.rotation = Quaternion.Euler(0, 90, 0);
        }
        if (Input.GetButtonDown("left")) {
            gameObject.transform.rotation = Quaternion.Euler(0, -90, 0);
        }
        if (Input.GetButtonDown("up")) {
            gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
        }
        if (Input.GetButtonDown("down")) {
            gameObject.transform.rotation = Quaternion.Euler(0, 180, 0);
        }
    }
}

Could you give me please any help on this? (Preferably with code, if you can :) )

1
So what problems do you have? Does it move? Do you get any error? - Programmer

1 Answers

1
votes

Assuming you want your character to move to where it is facing, and trying not to do a lot of changes to your code, this is what could be done:

First, attach both your script directly to your movable character.

Then, change your Move script to:

public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;

Vector3 startPos;
Vector3 endPos;

bool firstInput;
public bool justJump;

// Update is called once per frame
void Update () {
    if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
        if (perc == 1) {
            lerpTime = 1;
            currentLerpTime = 0;
            firstInput = true;
            justJump = true;
        }
    }
    startPos = gameObject.transform.position;

    if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
        endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
    }
    if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
        endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
    }

    if (firstInput == true) {
        currentLerpTime += Time.deltaTime * 5;
        perc = currentLerpTime / lerpTime;
        gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
        if (perc > 0.8f)  {
            perc = 1;
        }

        if (Mathf.Round(perc) == 1) {
            justJump = false;
        }
    }
}

}

And your AnimationController script to

public class AnimationController : MonoBehaviour {

    Animator anim;
    public GameObject thePlayer;

    // Use this for initialization
    void Start () {
        anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {
        Move moveScript = thePlayer.GetComponent<Move>();
        if (moveScript.justJump == true) {
            anim.SetBool("Jump", true);
        }
        else {
            anim.SetBool("Jump", false);
        }

        if (Input.GetButtonDown("right")) {
            gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
        }
        if (Input.GetButtonDown("left")) {
            gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
        }
    }
}

This will make your character rotate only when you press the left and right buttons, and move only when you press the up and down button. Althought this solves your problem, this is not the best way to write scripts for your character movement.

I recommend you read more about the state machine design patter. This book by Robert Nystrom has a chapter about it and is really easy to read. Also, its free to read online :)