Question by
KOR_APUcard · Apr 28, 2019 at 03:58 AM ·
scripting problem2d gametop down shooter
How to make movement speed decrease for 2D Top-down shooting game?
How to make movement speed decrease for 2D Top-down shooting game?
I have seen the Unity 2D UFO example. But that examples using "0 Gravity". I mean, like a space gravity.
However, I do not want "space gravity". So how to make speed decrease when I stop moving? (like a diep.io, etc.)
This is the script I am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public Rigidbody2D rigidbody2;
public bool zooming;
public float zoomSpeed;
public Camera raycamera;
public GameObject bulletSpawnPoint;
public float waitTime;
public GameObject bullet;
// Start is called before the first frame update
void Start()
{
rigidbody2 = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rigidbody2.AddForce(movement * speed);
RaycastHit hit;
Ray ray = raycamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Transform objectHit = hit.transform;
// Do something with the object that was hit by the raycast.
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Help! Create interactive button with an image + c# Script 0 Answers
IsTouchingLayers dont work : 0 Answers
In Unity 5.3, how do I detect a touch on a sprite in a 2D game? 2 Answers
Object reference not set to an instance of an object 0 Answers
My gameobject stops moving after entering the trigger?? 0 Answers