- Home /
Top Down 2D movement issue
Hello, I got a problem, I think it's no about code but with Unity, but for certainty I put here code. So my problem is when I start a game, the player start move backward (fall down it look like, yes and fixed by checking kinematics but I know the kinematic can be off and it should be working) and my bullets have more force when I'm shooting downward, I don't understand it, why it is doing this. Thanks for help.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float walkSpeed;
private float curSpeed;
public float maxSpeed;
public float sprintSpeed;
public float speed;
public Rigidbody2D rg2d;
public Rigidbody2D bullet;
//public Transform bulletSpawn;
public float bulletSpeed;
public float xValue;
public float yValue;
//private CharacterStat plStat;
void Start()
{
//plStat = GetComponent<CharacterStat>();
//walkSpeed = (float)(plStat.Speed + (plStat.Agility/5));
walkSpeed = speed;
sprintSpeed = walkSpeed + (walkSpeed / 2);
}
void FixedUpdate()
{
curSpeed = walkSpeed;
maxSpeed = curSpeed * 2;
if (Input.GetMouseButton (0)) {
Rigidbody2D bPrefab = Instantiate(bullet, new Vector3(transform.position.x + xValue, transform.position.y + yValue, transform.position.z), Quaternion.identity) as Rigidbody2D;
bPrefab.GetComponent<Rigidbody2D>().AddForce(transform.up * bulletSpeed);
}
// Move senteces
rg2d.velocity = new Vector2(Mathf.Lerp(0, Input.GetAxis("Horizontal")* curSpeed, 0.8f),
Mathf.Lerp(0, Input.GetAxis("Vertical")* curSpeed, 0.8f));
var MousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Quaternion qot = Quaternion.LookRotation (transform.position - MousePos, Vector3.forward);
transform.rotation = qot;
}
}
Answer by rutter · Nov 05, 2015 at 06:55 PM
Sounds to me like your top-down game is simulating gravity as if it were a side-scroller. The game thinks it's pushing objects "down", which from your perspective is "south".
You can remove gravity globally by setting Physics2D.gravity
to Vector2.zero
(see manual). You can also use this to control the amount and direction of gravity, but in your case it sounds like you'd want to remove it.
If you prefer, you could tell individual rigidbodies to ignore gravity by setting their gravityScale
to zero (see manual).
Thank you, I found the problem, the problem was in a Edit-Project Setting and Physics2D was gravity in Y setted to -9.81, that cause that my character was "falling down"
Your answer
Follow this Question
Related Questions
How to make Enemy shoot at Player Top Down 1 Answer
Instantiate after setting values? 0 Answers
shooting in 2d problem 0 Answers
Top down 2d shooting - XZ rotation 0 Answers