Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question First learning to code, what is going wrong?

Discussion in 'Getting Started' started by Akwinem, Jul 25, 2023.

  1. Akwinem

    Akwinem

    Joined:
    Dec 17, 2020
    Posts:
    1
    If I wedge the player object into the corner, it will sometimes start to climb the objects. Anyone know why?
    Here's the code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    [SerializeField] float MovementSpeed = 3f;
    public LayerMask collisionLayerMask;

    private bool IsCollidingNextFrame(Vector3 direction, float predictionTime)
    {
    // Predict the player's position in the next frame based on the speed and direction
    Vector3 predictedPosition = transform.position + direction * MovementSpeed * predictionTime;

    // Get the player's collider for its size
    Collider playerCollider = GetComponent<Collider>();
    Vector3 playerSize = playerCollider.bounds.size;

    // Calculate the player's half size for the BoxCast
    Vector3 playerHalfSize = 0.5f * playerSize;

    // Check for collisions using BoxCast
    RaycastHit hitInfo;
    bool isColliding = Physics.BoxCast(transform.position, playerHalfSize, direction, out hitInfo, transform.rotation, MovementSpeed * predictionTime, collisionLayerMask);

    return isColliding;
    }
    void Start()
    {

    }

    void Update()
    {
    float horizontalInput = Input.GetAxis("Horizontal");
    float verticalInput = Input.GetAxis("Vertical");
    Vector3 direction = new Vector3(horizontalInput, 0f, verticalInput);
    bool willCollideInNextFrame = IsCollidingNextFrame(direction, Time.deltaTime);
    if (!willCollideInNextFrame)
    {
    transform.Translate(direction * MovementSpeed * Time.deltaTime);

    }
    else
    {
    Debug.Log("Collision detected in the next frame.");
    }
    }
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please edit your post to use code-tags.

    Because the Rigidbdy cannot move the GameObject Transform because you're stomping over it. It's supposed to be in charge of it because it's simulating. Also, physics isn't running per-frame either by default, yet per-frame you're overwriting the Transform.

    Step back and consider how the Rigidbody can actually move the position if you're constantly moving it yourself. Either you choose to move by modifying the Transform or you let the Rigidbody move it. You cannot do both.

    If you've just started then I would consider following some physics movement tutorials as the forums are not well suited to getting this kind of information across.

    For this reason, I'll move your post to the Getting Started forum which is likely to be more useful to you.

    Good luck.