Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bladeofdismay · Mar 05, 2021 at 02:25 AM · rotationgameobjectquaternion

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;
         
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

204 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges