Question by
unity_51039 · May 29, 2019 at 05:53 AM ·
2dcontrolstopdowntop-down
2D Movement Controls Like a Dying Fly
I've recently started using unity out side of the occasional messing around, for school and have tried multiple things but can't get it to work as I always get the character Doing circles around the center of the screen.
The two scripts I'm using (I haven't finished the first one):
Character
using UnityEngine;
public class Go_Towards_Cursor : MonoBehaviour
{
public float speed = 5f;
public Vector2 Momentum;
private Vector2 NewPosition;
public float Acceleration;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//https://www.youtube.com/watch?v=mKLp-2iseDc
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed * Time.deltaTime);
//
if (Input.GetKey("w"))
{
Momentum.x = Mathf.Sin(transform.eulerAngles.z + 90) * Acceleration ;
Momentum.y = Mathf.Cos(transform.eulerAngles.z + 90) * Acceleration ;
}
NewPosition.x = transform.position.x + Momentum.x;
NewPosition.y = transform.position.y + Momentum.y;
transform.position = NewPosition;
}
}
Camera
using UnityEngine;
//https://youtu.be/MFQhpwc6cKE
public class Camera_Follow : MonoBehaviour
{
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
void LateUpdate() => transform.position = target.position + offset;
/*void LateUpdate()
{
transform.position = target.position + offset;
}*/
}
Comment