[Unitty C#] BUG z Colliderem
Ostatnio zmodyfikowano 2024-09-04 17:06
tBane Temat założony przez niniejszego użytkownika |
[Unitty C#] BUG z Colliderem » 2024-09-03 22:04:02 Witam. Jestem nowy w Unity i mam pewien problem, a mianowicie gdy postać koliduje z GameObjectem (drzewem) to tak jakby wchodzi na nie i nie wiem jak zrobić tak, by kolidowała i nie przesuwała się dalej. Video z BUG'iem: https://youtu.be/o0_jMWZfFboMam taki skrypt: using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UIElements;
public class PlayerController : MonoBehaviour { public Rigidbody rigidbody; public Animator animator; private float movementSpeed = 10.0f; private float rotationSpeed = 360.0f; private float animSpeed = 1.0f; private float move; private float rotate; private Vector3 movement; private Quaternion rotation = Quaternion.Euler( 0, 0, 0 ); void Start() { rigidbody = GetComponent < Rigidbody >(); animator = GetComponent < Animator >(); animator.Play( "idle" ); animator.speed = animSpeed; } void Update() { rotate = Input.GetAxis( "Horizontal" ); move = Input.GetAxis( "Vertical" ); movement = transform.forward * move * movementSpeed * Time.deltaTime; rotation = Quaternion.Euler( 0f, rotate * rotationSpeed * Time.deltaTime, 0f ); if( movement != Vector3.zero || rotation != Quaternion.Euler( 0.0f, 0.0f, 0.0f ) ) animator.Play( "walk" ); else animator.Play( "idle" ); } private void FixedUpdate() { rigidbody.MovePosition( rigidbody.position + movement ); rigidbody.MoveRotation( rigidbody.rotation * rotation ); } }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2024-09-03 22:45:04 using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine;
public class PlayerController : MonoBehaviour { public Rigidbody rigidbody; public Animator animator; public List < GameObject > objects = new List < GameObject >(); private float movementSpeed = 10.0f; private float rotationSpeed = 360.0f; private float animSpeed = 1.0f; bool collisioned; private float move; private float rotate; private Vector3 movement; private Quaternion rotation = Quaternion.Euler( 0, 0, 0 ); void Start() { rigidbody = GetComponent < Rigidbody >(); animator = GetComponent < Animator >(); animator.Play( "idle" ); animator.speed = animSpeed; } void Update() { rotate = Input.GetAxis( "Horizontal" ); move = Input.GetAxis( "Vertical" ); movement = transform.forward * move * movementSpeed * Time.deltaTime; rotation = Quaternion.Euler( 0f, rotate * rotationSpeed * Time.deltaTime, 0f ); collisioned = false; foreach( GameObject obj in objects ) if( collisionPrediction( this.gameObject, obj ) ) { collisioned = true; break; } if( movement != Vector3.zero || rotation != Quaternion.Euler( 0.0f, 0.0f, 0.0f ) ) animator.Play( "walk" ); else animator.Play( "idle" ); } private void FixedUpdate() { if( !collisioned ) rigidbody.MovePosition( rigidbody.position + movement ); rigidbody.MoveRotation( rigidbody.rotation * rotation ); } bool collisionPrediction( GameObject obj1, GameObject obj2 ) { Collider collider1 = obj1.GetComponentInChildren < Collider >(); Collider collider2 = obj2.GetComponentInChildren < Collider >(); if( collider1 != null && collider2 != null ) { Vector3 predictedPosition = collider1.transform.position + movement; Bounds predictedBounds = new Bounds( predictedPosition, collider1.bounds.size ); return predictedBounds.Intersects( collider2.bounds ); } return false; } }
|
|
tBane Temat założony przez niniejszego użytkownika |
» 2024-09-04 17:06:06 Problem nadal nie rozwiązany. Gdy postac wchodzi na obiekt, ta sie przewraca i nie wiem jak to zrobić, by normalnie wchodziła na obiekt. https://youtu.be/7lj3ZDsTotc |
|
« 1 » |