when I run this my weapon should face the direction of my mouse but it does that for like 1second and it stops
and it doesn’t face the position of my mouse anymore
what should I do?
Don’t place a screenshot of your code, use a CODE tag instead. It’s described in the topic at the very top:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weapon : MonoBehaviour
{
public float offset;
public GameObject projecttile;
public Transform shotpoint;
private float timebtwshots;
public float starttimebtwshots;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ+ offset);
Debug.Log("moveing");
if (timebtwshots <= 0)
{
if (Input.GetMouseButtonDown(0))
{
Instantiate(projecttile, shotpoint.position, transform.rotation);
timebtwshots = starttimebtwshots;
}
}
else
{
timebtwshots -= Time.deltaTime;
}
}
}
here
Does it still happen if you remove the offset?
yes it still don’t work and I just found out that when I move my character It turns properly but when I stay still I wouldn’t move
This line of code:
transform.rotation = Quaternion.Euler(0f, 0f, rotZ+ offset);
You are setting the rotation every frame, whether the mouse is moving or not. So when the mouse stops moving, the rotation is going to snap back to (0,0,offset) regardless of where it was.
then what should I do?