Jump to content
Unity Insider Forum
Sign in to follow this  
Damian izz back

Möchte das mein Charakter nur Vorwärts laufen kann

Recommended Posts

Guten Abend

Ich habe mir ein Movement-Script erstellt und es funktioniert einwandfrei. Aber könnte mir jemand verraten was ich im Code ändern muss, damit der Charakter nur noch vorwärts laufen kann und nicht mehr rückwärts? Könnte mir evtl. jemand weiterhelfen? 

Gruss

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Playermovement : MonoBehaviour
{
    public float speed = 6.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public float rotateSpeed = 15.0f;
    private Vector3 moveDirection = Vector3.zero;
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded)
        {

            moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            if (Input.GetButton("Jump"))
                moveDirection.y = jumpSpeed;


        }
        moveDirection.y -= gravity * Time.deltaTime;
            controller.Move(moveDirection * Time.deltaTime);

        //Rotate Player
        transform.Rotate(0, Input.GetAxis("Horizontal"), 0);

    }
}

 

Share this post


Link to post
Share on other sites

Wenn bei deinem Spiel:

Input.GetAxis("Vertical") > 0

bedeutet, dass dein Spieler vorwärts läuft, dann hast die Möglichkeit mit

Mathf.Abs(Input.GetAxis("Vertical"))

immer einen positiven Wert zu erhalten und dementsprechend vorwärts zu laufen.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...