my object is rotating slow towards the mouse and moving towards to mouse as well (i have this part done) it rotates slow but move in a straight line i want it to sync it movement with the rotation so when its rotating the movement shouldn’t be straight it should be facing the rotation in curve line
i will give the code if needed
Both the Rotation and the Move function have the mouse position as their target, so both of them are moving there. Change the target of the Move function to a simple forward vector and let the Rotation be in charge of finding the mousepos.
can you show me a example code?
Show me yours, i’ll show you mine
Edit:
Maybe this is enough to give you an idea: Somewhere in your code you tell the Object to move towards your mousePos.
Replace mousePos with: transform.forward * Time.deltaTime * movementSpeed
You have to set the movementSpeed of course. Maybe even build a function to set the speed according to the distance left.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public GameObject Bullet;
public Transform shotPoint;
Rigidbody2D rb;
public float moveSpeed = 30;
public float speed = 200;
public float offset;
private float timeBtwShots;
public float startTimeBtwShots;
void Awake()
{
rb = this.GetComponent();
}
void Update()
{
AimAndIns();
GoTowardsMouse();
}
void AimAndIns()
{
Vector3 lookPos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
Quaternion qTO = Quaternion.Euler(0f, 0f, rotZ + offset);
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTO, speed * Time.deltaTime);
if (timeBtwShots <= 0)
{
if (Input.GetMouseButton(1))
{
Instantiate(Bullet, shotPoint.position, transform.rotation);
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
void GoTowardsMouse()
{
rb.velocity = Vector2.zero;
if (Input.GetMouseButton(0))
{
Vector3 targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPos.z -= transform.position.z;
//transform.position = Vector2.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
rb.MovePosition(Vector2.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime));
}
}
}
GoliathAT:
Show me yours, i’ll show you mine
Edit:
Maybe this is enough to give you an idea: Somewhere in your code you tell the Object to move towards your mousePos.
Replace mousePos with: transform.forward * Time.deltaTime * movementSpeed
You have to set the movementSpeed of course. Maybe even build a function to set the speed according to the distance left.
can you show me where exacly in the code?