Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Unitty C#] BUG z Colliderem

Ostatnio zmodyfikowano 2024-09-04 17:06
Autor Wiadomość
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_jMWZfFbo

Mam taki skrypt:
C/C++
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 );
   
   
// Start is called before the first frame update
   
void Start()
   
{
       
rigidbody = GetComponent < Rigidbody >();
       
animator = GetComponent < Animator >();
       
animator.Play( "idle" );
       
animator.speed = animSpeed;
   
}
   
   
// Update is called once per frame
   
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 );
       
       
// Animacje
       
if( movement != Vector3.zero || rotation != Quaternion.Euler( 0.0f, 0.0f, 0.0f ) )
           
 animator.Play( "walk" );
       
else
           
 animator.Play( "idle" );
       
   
}
   
   
private void FixedUpdate()
   
{
       
// Movement and Rotation
       
rigidbody.MovePosition( rigidbody.position + movement );
       
rigidbody.MoveRotation( rigidbody.rotation * rotation );
   
}
}
P-181555
tBane
Temat założony przez niniejszego użytkownika
» 2024-09-03 22:45:04
C/C++
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()
   
{
       
// values of rotate and move
       
rotate = Input.GetAxis( "Horizontal" );
       
move = Input.GetAxis( "Vertical" );
       
       
// calculate the vectors of movement and rotation
       
movement = transform.forward * move * movementSpeed * Time.deltaTime;
       
rotation = Quaternion.Euler( 0f, rotate * rotationSpeed * Time.deltaTime, 0f );
       
       
// collisions predictions
       
collisioned = false;
       
foreach( GameObject obj in objects )
       
if( collisionPrediction( this.gameObject, obj ) )
       
{
           
collisioned = true;
           
break;
       
}
       
       
// Check the animations
       
if( movement != Vector3.zero || rotation != Quaternion.Euler( 0.0f, 0.0f, 0.0f ) )
           
 animator.Play( "walk" );
       
else
           
 animator.Play( "idle" );
       
   
}
   
   
private void FixedUpdate()
   
{
       
// Movement
       
if( !collisioned )
           
 rigidbody.MovePosition( rigidbody.position + movement );
       
       
// Rotation
       
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 )
       
{
           
// Simulate new position after movement
           
Vector3 predictedPosition = collider1.transform.position + movement;
           
           
// Create test Bounding Box
           
Bounds predictedBounds = new Bounds( predictedPosition, collider1.bounds.size );
           
           
// Check the collision with new position
           
return predictedBounds.Intersects( collider2.bounds );
       
}
       
return false;
   
}
}
P-181556
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
P-181557
« 1 »
  Strona 1 z 1