- Home /
Character moving in the wrong direction, glitched Rigidbody?
I'm developing a runner game, and I'm having a problem to properly set the character movement. Now, just for reference, the game uses 3D graphics, but 2D movement, I'm using this tutorial as base, only modifying the scripts so they can work on a 3D environment: https://www.youtube.com/playlist?list=PLiyfvmtjWC_XmdYfXm2i1AQ3lKrEPgc9- (the first two videos). The script is like this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NaomiMovement : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public Rigidbody myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
private Collider myCollider;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody>();
myCollider = GetComponent<Collider>();
}
// Update is called once per frame
void Update()
{
grounded = Physics.IsTouchingLayers(myCollider, whatIsGround);
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown (0))
{
if(grounded)
{
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
}
}
}
}
The character is supposed to move to the right, but it's moving to the left instead, and upon adding the rigidbody, she just go through the ground! Selecting "Kinematic" hinders her movement completely...
What's happening!?
Answer by Vini310 · Jun 16, 2020 at 06:59 PM
I managed to fix the problem by negativating moveSpeed public class NaomiMovement : MonoBehaviour { public float moveSpeed; public float jumpForce;
public Rigidbody myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
private Collider myCollider;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody>();
myCollider = GetComponent<Collider>();
}
// Update is called once per frame
void Update()
{
myRigidbody.velocity = new Vector2(-moveSpeed, myRigidbody.velocity.y);
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown (0))
{
if(grounded)
{
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
}
}
Debug.Log(myRigidbody.velocity.y);
Debug.Log(myRigidbody.velocity.x);
}
}
Now I just need to prevent the character from jumping into infinite...