- Home /
Object rotating incorrectly
I am making a top-down melee-based game and am trying to implement a sword slash effect when I click. When the effect is instantiated, it always spawns in the top right corner of my player, and always with the same rotation. In my code, I have a game object set to rotate to my mouse position, so that the effect is facing the cursor. After the effect is spawned, it will rotate alongside the parent rigidbody it's attached to, it just doesn't spawn in the correct position or with proper rotation. I am trying to use Quaternion.Euler to adjust the spawn angle, but it rotates the effects incredibly wrong. If I manually set the x rotation to, say, 90, it'll spawn the effects rotated at 90 degrees. If I use my angle
variable to do the same thing, it rotates the x to about what angle
is, but not quite. It will also rotate the Y and Z axes to about half of the calculated value, even though they are set to 0. Why is this happening? Here's the player movement script I'm using.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Rigidbody2D rbSwipe;
public Camera cam;
public Animator animator;
public GameObject swipe;
public Transform swipePoint;
private bool allowSwipe = true;
private float swipeDelay = 0.1f;
private Vector2 swipePos;
float animationSpeed = 0f;
Vector2 movement;
Vector2 mousePos;
public void Start()
{
//animator.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
swipePos = GameObject.FindGameObjectWithTag("SwipePoint").transform.position;
swipe.transform.localPosition = swipePos;
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetButtonDown("Fire1") && (allowSwipe))
{
StartCoroutine(Swipe());
}
}
IEnumerator Swipe()
{
allowSwipe = false;
Vector3 swipeDir = mousePos - rb.position;
float angle = Mathf.Atan2(swipeDir.y, swipeDir.x) * Mathf.Rad2Deg;
Debug.Log("angle is: " + angle);
Instantiate(swipe, new Vector3(swipePoint.transform.position.x, swipePoint.transform.position.y, swipePoint.transform.position.z), Quaternion.Euler(angle, 0, 0), swipePoint);
Destroy(GameObject.FindGameObjectWithTag("Swipe"), 10f);
yield return new WaitForSeconds(swipeDelay);
allowSwipe = true;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
rbSwipe.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
Vector2 swipeDir = mousePos - rbSwipe.position;
float angle = Mathf.Atan2(swipeDir.y, swipeDir.x) * Mathf.Rad2Deg - 90f;
rbSwipe.rotation = angle;
}
}
Your answer
Follow this Question
Related Questions
How to get angular difference? 2 Answers
Camera rotation the same as player rotation 1 Answer
Recording hand gestures and playing it back on same hand model 1 Answer
Rotating a GameObject based on another GameObject's y-axis 1 Answer
How do I make create a rotateable object that follows a raycast hit point? 2 Answers