Search Unity

Help Wanted How do I stop walking through my floor?

Discussion in 'Editor & General Support' started by lz7cjc, Oct 19, 2021.

  1. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    406
    2021-10-19 (2).png 2021-10-19 (3).png 2021-10-19 (5).png Hi
    I have a small script which allows me to walk around my scene. However if I point downwards or walk into a tree, i go through the asset.
    (terrain collider is switched on on my terrain and I have added mesh collider to the "player" object which contains the camera)
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class VRLookWalk : MonoBehaviour
    7. {
    8.     public Transform vrCamera;
    9.     public float speed = 3.0f;
    10.     private bool moveNow = false;
    11.     public bool mouseHover = false;
    12.     public bool move = false;
    13.     public float counter = 0;
    14.     public float SecondsToDelay = 1;
    15.     public float speedSet;
    16.     public bool increaseSpeed;
    17.     public bool decreaseSpeed;
    18.     public GameObject stopSign;
    19.     public GameObject faster;
    20.     public GameObject slower;
    21.     public GameObject startSign;
    22.     //stop buggy after x seconds
    23.     public float DelayStop;
    24.     public float Delay;
    25.     //stop buggy after x seconds
    26.     private bool toggler = false;
    27. private bool turnOff = false;
    28.     private bool turnOn = false;
    29.     public void Update()
    30.     {
    31.         if (mouseHover)
    32.         {
    33.             //Debug.Log("speed is" + speedSet + " because I am moving:" + move);
    34.             counter += Time.deltaTime;
    35.             if (counter < Delay && !move)
    36.             {
    37.                 counter += Time.deltaTime;
    38.             }
    39.             else if (counter >= Delay && !toggler)
    40.             {
    41.                 toggler = !toggler;
    42.                 move = !move;
    43.                 speedSet = speed;
    44.  
    45.             }
    46.             else if (counter < DelayStop && move)
    47.             {
    48.                 counter += Time.deltaTime;
    49.             }
    50.             else if (counter >= DelayStop && !toggler)
    51.             {
    52.                 toggler = !toggler;
    53.                 move = !move;
    54.  
    55.                 speedSet = 0;
    56.  
    57.             }
    58.         }
    59.         if (speedSet > 0)
    60.         {
    61.             LetsGo();
    62.         }
    63.     }
    64.     // mouse Enter event
    65.     public void OnMouseEnter()
    66.     {
    67.         mouseHover = true;
    68.     }
    69.     public void OnMouseExit()
    70.     {
    71.         mouseHover = false;
    72.         toggler = false;
    73.         counter = 0;
    74.     }
    75.  
    76.  
    77.  
    78.     public void SpeedUp()
    79.     {
    80.         turnOn = true;
    81.         if (turnOn)
    82.         {
    83.             speed = speedSet + 0.2f;
    84.             Debug.Log("speeding up is: " + speed);
    85.         }
    86.         turnOn = false;
    87.     }
    88.     public void SlowDown()
    89.     {
    90.         turnOn = true;
    91.         if (turnOn)
    92.         {
    93.             speed = speedSet - 0.2f;
    94.             Debug.Log("slowing down  is: " + speed);
    95.  
    96.         }
    97.         turnOn = false;
    98.     }
    99.    
    100.         public void LetsGo()
    101.     {
    102.         stopSign.SetActive(true);
    103.         startSign.SetActive(false);
    104.         transform.position = transform.position + Camera.main.transform.forward * speed * Time.deltaTime;
    105.  
    106.  
    107.  
    108.         //}
    109.     }
    110.  
    111.  
    112.  
    113. }
    114.  
    115.  
    116.  
    any help gratefully received :)
    thanks
     
    Last edited: Oct 19, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,204
    What exactly are you doing to keep yourself out of the ground? I see no Rigidbody involved here.

    If you are using a rigidbody...

    ... anytime you manipulate transform.position directly (via any means), you are bypassing the physics system and you can reasonably expect glitches and collisions to fail and misbehave.

    Instead, ONLY use .MovePosition() and .MoveRotation() on the Rigidbody to move Rigidbody-equipped GameObjects.
     
  3. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    406
    thanks - will give it a go
    i have a rigidbody on the Player which contains the camera and the terrain - i have turned off gravity on both
    should it be anywhere else?
     
  4. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    406
    i can't find .MovePosition() in the documentation - how do i implement in my code
    1. transform.position = transform.position + Camera.main.transform.forward * speed * Time.deltaTime;
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    20,204
  6. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    406
    ah i see it now - thanks for your help
    think I have got it working
     
    Kurt-Dekker likes this.
unityunity