Search Unity

Player moving in a way I did not want, Need help fixing it. Pic and codes

Discussion in 'Scripting' started by JPF55, Apr 30, 2015.

  1. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    Hello, I got my player to move but when the player gets so far it messes up and they go through the plane and I have no idea how to fix it.
    The plane has a mesh collider and the player still goes through it.
    Here is an image of what I mean (I'm not the best at drawing)
    And I have posted my code as well.
    Example1.jpg
     

    Attached Files:

  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public int speed;
    8.     public int fSpeed;
    9.     public int rSpeed;
    10.     public GameObject GameOver;
    11.  
    12.     void Update()
    13.     {
    14.         transform.Rotate(new Vector3(15, 0, 0) * Time.deltaTime * rSpeed);
    15.     }
    16.  
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         float moveHorizontal = Input.GetAxis ("Horizontal");
    21.  
    22.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, fSpeed);
    23.  
    24.         rigidbody.velocity = movement * speed;
    25.     }
    26.  
    27.     void OnTriggerEnter(Collider other)
    28.     {
    29.  
    30.         if(other.gameObject.tag == "Wall")
    31.         {
    32.             Time.timeScale = Time.timeScale == 0 ? 1: 0;
    33.             GameOver.SetActive(true);
    34.         }
    35.     }
    36.  
    37. }
    38.  
    I've pasted your code here to save others who might be able to help you from having to download & view the file as I just did. I'm at the end of a long day and can't say that I have a solution for you (though I will look again closer when I get up tomorrow), however I am a bit curious about where your "fSpeed" is getting a value? I don't see it assigned anywhere in your code.

    If I'm interpreting your code / intentions correctly, you're trying to move the object back and forth on the X axis based on "Horizontal" input, while also continually rotating on the X axis as well? Please explain what you're actually trying to do, in words rather than pictures, so that we can understand a bit better. Is this a 2d game? Are we looking at it from the top down? Regardless, you only appear to be affecting your character on a single axis, but it seems that you actually want it to move in 2 directions (X/Y, X/Z, Y/Z, depending of course on your intentions & setup). Happy to help once I've got more to go on :)
     
  3. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    The game is 3D, the fspeed is a public variable that i control outside the code in unity, the rotation i dont need anymore, the player is always moving forward down a pipe but the only controls the user has is to move the player left and right, but i need the player to move around in a circle continuously to the left or to the right without breaking through the plane.
     
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    OK, so your player control is now moving on the X and Z axis with fSpeed being declared elsewhere? So long as both X and Z are getting proper values when they need them, I'm at a loss for what the problem might be. Granted, every situation is different, and I'm still likely missing something in your setup that would perhaps explain things better. I wish I could be of further assistance, but there really isn't much to go on here...
     
  5. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    When the player goes so far to the left or right they end up going through the plane
     
  6. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    Okay, well one option would be to simply limit the character's movement through code so that it's not possible for it to move beyond a certain point. For example, let's say that you don't want the character to be able to move any further on the X axis than -50.0 and 50.0. Here's how you could do that:

    Code (CSharp):
    1. public int minX = -50.0f; // farthest left the player can go
    2. public int maxX = 50.0f; // farthest right the player can go
    3.  
    4. void Update()
    5. {
    6.     if (transform.position.x < minX) // if the player is beyond minX
    7.    {
    8.        transform.position = new Vector3 (minX, transform.position.y, transform.position.z); // put the player back at minX on the X axis
    9.    }
    10.  
    11.    if (transform.position.x > maxX) // if the player is beyond maxX
    12.    {
    13.        transform.position = new Vector3 (maxX, transform.position.y, transform.position.z); // put the player back at maxX on the X axis
    14.    }
    15.  
    16. } // end of Update()
    It's just an example (and untested as I'm on someone else's PC right now), but that should keep the player from moving too far one way or another on the X axis. Let me know if that helps any :) If not, I'm happy to try again haha.
     
    Last edited: May 2, 2015
  7. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    First of all, I would suggest that you put the rotation and the motion together or else you would end up not moving in a loop.

    Update and FixedUpdate has got different time frames and gets called at different intervals, so you might be facing at an undesired direction when you move forward. which would explain your drawings behavior.

    Like let's say there might be 2 Update calls between FixedUpdate duration in one instance and then, there may be 5 Update calls between another FixedUpdate duration. The change in facing would turn out to be different when you move.
     
    krougeau likes this.
  8. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    I got it to work, I made an empty game object and parented it to the player object and just rotated it so the player object moves in a continues circle. Now all I need to do is get the camera to work and I'll be fine. Thanks for the help
     
    krougeau likes this.
  9. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    Congrats! Glad you managed to work it out. Sorry my answers weren't of much use to you, but I tried :) Have fun!
     
  10. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    I used an empty game object, and had it be the parent to the player object. So the user controls the rotation of the empty game object. And thanks to everyone who posted a reply
     
    krougeau likes this.