- Home /
Sprite is not stopping when it hits the colliders
I am creating a laser defence game and I have two questions.
Question 1: The player sprite is moving just fine, however, if I keep the left or right arrow key pressed it will just keep moving and gets out of the game screen. I did put two colliders on each side but the sprite just keeps moving like there is nothing there.
Question 2 : In order to keep the player in place I have to put a collider underneath it (this collider is the only one that works). Is there a way where I can just keep the player exactly where I want it instead of just using a collider?
Can anyone tell me how to solve these ? Thank you.
Player Script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float Speed;
private Vector2 MoveVelocity;
private Rigidbody2D RigidBody;
// Use this for initialization
void Start () {
RigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
//Gathering user input
//When the user presses the left key it will give a value of a -1 and when the user presses the right key it will give a value of 1.
Vector2 MoveInput = new Vector2(Input.GetAxisRaw("Horizontal"),0f); //The GetAxisRaw is used for in other for the player to stop exactly when the user has let go the arrow keys
MoveVelocity = MoveInput * Speed;
}
void FixedUpdate() //To move our player
{
//If for example the player is at 10 Speed and the user presses the right key the player will move to (10,0).
RigidBody.MovePosition(RigidBody.position + MoveVelocity * Time.fixedDeltaTime);
}
}
Your answer
Follow this Question
Related Questions
,How to stop jump animtion 1 Answer
Next Scene not loading after all enemies are destoryed 0 Answers
Rewind not working,Rewind not working 0 Answers