Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Walking interation between Player and Terrain works, but not the opposite

Discussion in 'Physics' started by Mich-C, Apr 17, 2021.

  1. Mich-C

    Mich-C

    Joined:
    Feb 18, 2021
    Posts:
    1
    Hi, I downloaded this First Person Control free asset. (https://assetstore.unity.com/packages/tools/input-management/mini-first-person-controller-174710) Basically, the player is a capsule.

    For my game, I need a player with a fixed position and the game world (terrain and models) that moves around it.
    I accomplished this by deactivating the movement script for the player and adding the same script to the game world.
    I also put a minus in the "speed" field so the movement with arrows is not inverted.

    If the player walks on flat terrain the thing works, :) but if he walks on a higher/lower part terrain, the terrain doesn't move up and down.:( (also the jump only works on flat terrain).

    In a "standard" movement, if an object like a capsule moves on terrain, the capsule moves up and down according to the terrain height, but the opposite doesn't seem to work... probably I'm missing something from the Unity gravity/collision system. (Maybe a Terrain is a fixed game object that can't be moved at all).


    Sorry but this time I have no idea how to solve this.
    Should I remove the Terrain Collider and use another collider for my Terrain? Or should I add/invert anything in the movement script?

    Here are the movement script and the jump script from the asset:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class FirstPersonMovement : MonoBehaviour
    5. {
    6.     public float speed = 5;
    7.     Vector2 velocity;
    8.  
    9.     [Header("Running")]
    10.     public bool canRun = true;
    11.     public bool IsRunning { get; private set; }
    12.     public float runSpeed = 9;
    13.     public KeyCode runningKey = KeyCode.LeftShift;
    14.     /// <summary> Functions to override movement speed. Will use the last added override. </summary>
    15.     public List<System.Func<float>> speedOverrides = new List<System.Func<float>>();
    16.  
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         // Move.
    21.         IsRunning = canRun && Input.GetKey(runningKey);
    22.         float movingSpeed = IsRunning ? runSpeed : speed;
    23.         if (speedOverrides.Count > 0)
    24.             movingSpeed = speedOverrides[speedOverrides.Count - 1]();
    25.         velocity.y = Input.GetAxis("Vertical") * movingSpeed * Time.deltaTime;
    26.         velocity.x = Input.GetAxis("Horizontal") * movingSpeed * Time.deltaTime;
    27.         transform.Translate(velocity.x, 0, velocity.y);
    28.     }
    29. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Jump : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     GroundCheck groundCheck;
    7.     Rigidbody rigidbody;
    8.     public float jumpStrength = 2;
    9.     public event System.Action Jumped;
    10.  
    11.  
    12.     void Reset()
    13.     {
    14.         groundCheck = GetComponentInChildren<GroundCheck>();
    15.         if (!groundCheck)
    16.             groundCheck = GroundCheck.Create(transform);
    17.     }
    18.  
    19.     void Awake()
    20.     {
    21.         rigidbody = GetComponent<Rigidbody>();
    22.     }
    23.  
    24.     void LateUpdate()
    25.     {
    26.         if (Input.GetButtonDown("Jump") && groundCheck.isGrounded)
    27.         {
    28.             rigidbody.AddForce(Vector3.up * 100 * jumpStrength);
    29.             Jumped?.Invoke();
    30.         }
    31.     }
    32. }


    Thanks in advance for any help! ;)