wie kann ich meinen Spieler in unity konstant nach vorne bewegen?
Ich möchte in meinem Spiel (Unity) den Spieler konstant nach vorne bewegen. Wie funktioniert das ohne rigidbody. Ich kann nämlich keinen hinzufügen weil der Spieler sonst mit nichts kollidieren kann, da ich einen Character Controller benutze.
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
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:
Hat leider nicht funktioniert, ich werde immer zurückgesetzt.