wie kann ich meinen Spieler in unity konstant nach vorne bewegen?

1 Antwort

movementSpeed = 10f;
void Update()
{
  transform.position += new Vector3(0, 0, 10 * Time.deltaTime);
}

Das sollte passen. je nachdem in welche Richtung er sich bewegen soll, musst du

10 * Time.deltaTime

an die x, y oder z Stelle des Vector3 einsetzen. movementSpeed kannst du natürlich auch ohne Probleme verändern

Woher ich das weiß:Hobby

Mapri15 
Beitragsersteller
 18.01.2020, 14:06
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
  CharacterController characterController;

  public float speed = 6.0f;
  public float jumpSpeed = 8.0f;
  public float gravity = 20.0f;
  public Joystick joystick;
  public float forwardForce;
  private Vector3 moveDirection = Vector3.zero;

  void Start()
  {
    characterController = GetComponent<CharacterController>();
  }

  private void Update()
  {
    transform.position += new Vector3(0, 0, forwardForce * Time.deltaTime);
  }

  void FixedUpdate()
  {

    if (characterController.isGrounded)
    {
       

      moveDirection = new Vector3(joystick.Horizontal, 0.0f, joystick.Vertical);
      moveDirection *= speed;

      
    }

     
    moveDirection.y -= gravity * Time.deltaTime;

     
    characterController.Move(moveDirection * Time.deltaTime);
  }
}

Hat leider nicht funktioniert, ich werde immer zurückgesetzt.

0
MistrX271  18.01.2020, 14:49
@Mapri15

Wenn du einen ganz normalen Character Controller brauchst, dann kann ich dir den empfehlen:

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;
    public float walkspeed = 12f;
    public float rundSpeed = 24f;
    public float crouchSpeed = 6f;
    public float speed = 12f;
    public float jumpHeight = 3f;
    private Vector3 velocity;
    public float gravity = -9.81f;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    bool isGrounded;
    private void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        if (isGrounded && velocity.y < 0f)
        {
            velocity.y = -2f;
        }
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);
        if (isGrounded && Input.GetButtonDown("Jump")) 
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
        }
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}

Ist aus einem Tutorial von Brackeys und tut, ohne Rigidbody, was er soll

Hier ist auch noch ein MouseLook Script:

public class MouseLook : MonoBehaviour
{
    public float mouseSensivity = 100f;
    private float xRotation = 0f;
    public Transform playerBody;
    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensivity * Time.deltaTime;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

Dafür musst du ein Empty Object mit einem chracter Controller und dem Movement Script ausstatten. Dem Objeckt musst du dann noch eine Kamera, ein Ground Check Objekt (um zu prüfen, ob der Boden berührt wird), und einen Cylinder als "Körper" hinzufügen. Das GroundCheck Objekt muss am besten an die "Füße", also weit nach unten positioniert werden. Jetzt nur noch der Kamera das mouseLook Script hinzufügen und die Variablen eintragen oder per Drag n Drop ausfüllen, und alles sollte passt.

Hier der Link zum Video:

https://www.youtube.com/watch?v=_QajrabyTJc&t=

0
Mapri15 
Beitragsersteller
 18.01.2020, 13:56

Danke! Ich Probiere es gleich aus!

0