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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

3D object to move left and right only? Mesh collider not working also!#

Discussion in 'Scripting' started by CoopReed, Dec 2, 2015.

  1. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    I've been following the procedural cave generation tutorial and recently uncovered some problems. My player come towards the camera, how would I make it go left and right, also at speed the mesh colliders do not work?

    This is my code for the player script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControl : MonoBehaviour {
    5.  
    6.     Rigidbody rigidbody;
    7.     Vector3 velocity;
    8.  
    9.    
    10.     void Start () {
    11.         rigidbody = GetComponent<Rigidbody>();
    12.     }
    13.    
    14.    
    15.     void Update () {
    16.  
    17.         this.transform.Translate(Input.GetAxis("Horizontal"), 0, 0);
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         rigidbody.MovePosition(rigidbody.position + velocity * Time.fixedDeltaTime);
    23.     }
    24.  
    25. }
    26.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why are you translating and altering the position of the rigidbody...???
     
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    if you want to move it left and right you should not only change the x axis but also the z position...
    Code (CSharp):
    1. void Update () {
    2.         this.transform.Translate(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    3. }
    second point, same as @LeftyRighty said, wy?

    and because of collision, where's your OnCollisionEnter code ? google for it ;)
     
  4. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    I used that because i thought it would be better, im new to this kinda thing. Also the collider script is in my mesh generator.
     
  5. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Nevermind guys just figured out the collider part. Thanks anyway

    (It was because my object was floating forward )