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

C# Radial Gravity Scripts Causing Player Controller Issues

Discussion in 'Scripting' started by FleshKnot, Jun 26, 2014.

  1. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    Hello Everyone. So I'm currently working on a system that is used to create radial gravity so that gravity can be used on a spherical planet like surface. The issue that I'm having is that once all of the scripts and objects are in place, the player object "pops" to the coordinates 0,0,0 and is stuck there even if the controls are used or if you try to manually pull it. I'll paste the scripts for the player and the gravity attractor. The Faux Gravity Attractor goes onto the mass creating the gravity and the other two attach to the player character object. Hopefully we can fix this.
     

    Attached Files:

  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    The problem seems to be in your PlayerController.cs. Right here you set moveDir...

    Code (csharp):
    1. moveDir = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")).normalized;
    When neither the horizontal or the vertical are being used, moveDir will be set to (0,0,0)...and then in the FixedUpdate...

    Code (csharp):
    1. rigidbody.MovePosition(rigidbody.position = transform.TransformDirection(moveDir) * moveSpeed * Time.deltaTime);
    You'll set his position to the world space of (0,0,0) times moveSpeed * Time.deltaTime, which is zero. I don't think this is what you're trying to do.
     
  3. Pati-Co

    Pati-Co

    Joined:
    Jan 9, 2014
    Posts:
    56
  4. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    What could I do to fix this? The fixed update is so that the object is always oriented with the gravitational attractor...
     
  5. FleshKnot

    FleshKnot

    Joined:
    May 4, 2014
    Posts:
    67
    Never mind I fixed it, in my void update I had rigidbody.position = transform instead of rigidbody.position + transform.,,