Good day to you all.
I’m relatively new to unity 2d and I am trying to create a game where the player moves from one place to another by point-and-clicking with the mouse. I would like to add an acceleration to when they start moving from 0 to MaxSpeed and from MaxSpeed to 0 when they get close to their destination, instead of an instant acceleration.
I am kind of hoping to not require forces to achieve this.
My code currently looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BotMover : MonoBehaviour
{
[SerializeField] private Camera mainCamera;
private Vector3 targ;
public float MaxSpeed = 3f;
float currentSpeed = 0f;
public float accelFactor = 0.02f;
void Start()
{
targ = transform.position;
}
// Update is called once per frame
void Update()
{
PositionalMover(MaxSpeed);
//currentSpeed = 0f;
}
void PositionalMover(float s){
if(Input.GetMouseButtonDown(0)){
targ = mainCamera.ScreenToWorldPoint(Input.mousePosition);
targ.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, targ, s * Time.deltaTime);
}
}