Unity – Cinemachine-CharakterbewegungC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Unity – Cinemachine-Charakterbewegung

Post by Anonymous »

Ich bin also neu bei Unity und habe diesen Code in meinem Player-Objekt:
Ich bemerke, dass sich die Bewegung meines Charakters umkehrt, während sich meine Kamera dreht. Ich denke, dass es XYZ von der Welt aus folgt und sich nicht an die Kamerarotation anpasst.

Code: Select all

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

public class PlayerController : MonoBehaviour
{
public ChangeStates changeStates;

public float speed;
public float jumpForce;
private float horizontalInput;
private float forwardInput;
private Rigidbody playerRb;

public bool isOnGround = true;

public SpriteRenderer sr;
// Start is called before the first frame update

void Start()
{
playerRb = GetComponent();
changeStates = GetComponentInChildren();
}

// Update is called once per frame
void Update()
{

// Player Inputs
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");

// Move forward
transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);

// Jump
if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
{
playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}

//Sprite gira
if (horizontalInput != 0 && horizontalInput < 0)
{
sr.flipX = true;
}
else if (horizontalInput != 0 && horizontalInput > 0)
{
sr.flipX = false;
}
}

private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Terrain"))
{
isOnGround = true;
changeStates.StopJumpingAnimation();
}
}
}
Ich weiß nicht, wie ich das lösen soll. Wie mache ich das?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post