Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 IgotbulletZ · Jul 22, 2019 at 08:38 PM · rotation2d-physics

My object is rotating crazily - 2D

Hi, I want my object to simply rotate and look towards where I click (In 2D), but instead it kind of faces my click in 3D, so it rotates around the X and Y axis instead of just Z. I have tried many different types of rotation strategies, but none seem to work the way I want them to. If anyone could help, that would be greatly appreciated. Cheers!

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class PlayerShip : MonoBehaviour {

 public Text massText;
 public Text thrustText;

 [Header("Attributes")]

 public float speed = 0f;
 public float mass = 1f;

 [Space(5)]

 public Rigidbody2D rb;

 [Space(5)]

 public float distanceToClick;
 private Vector3 mouseClickPos;

 [Space(5)]

 public float smoothTime;
 private Vector3 velocity = Vector3.zero;

 void Start()
 {       
     rb = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame
 void Update()
 {
     GameObject placementManager = GameObject.Find("PlacementManager");

     if (placementManager.GetComponent<PlacementScript>().isAnObjectSelected == true)
     {
         transform.rotation = Quaternion.Euler(0, 0, 90);
         transform.position = new Vector2(Mathf.Round(transform.position.x), Mathf.Round(transform.position.y));
     }

     mass = mass / 2;
     rb.mass = mass;
     smoothTime = rb.mass;

     float massRounded = Mathf.Round(mass);
     float thrustRounded = Mathf.Round(speed);

     massText.text = "Mass: " + massRounded;
     thrustText.text = "Thrust: " + thrustRounded;
 }

 private void FixedUpdate()
 {
     GameObject placementManager = GameObject.Find("PlacementManager");

     if (Input.GetMouseButton(1))
     {
         if (placementManager.GetComponent<PlacementScript>().isAnObjectSelected == false)
         {
             mouseClickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

             distanceToClick = Vector3.Distance(transform.position, mouseClickPos);
         }
     }

     if (distanceToClick >= 0.5f && speed > 0)
     {
         if (placementManager.GetComponent<PlacementScript>().isAnObjectSelected == false)
         {
             Vector2 dir = mouseClickPos - transform.position;
             var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

             transform.position = Vector3.SmoothDamp(transform.position, mouseClickPos, ref velocity, smoothTime);
         }
     }

     transform.position = new Vector3(transform.position.x, transform.position.y, 0);
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JonPQ · Jul 22, 2019 at 09:56 PM

did you try Transform.RotateAround() ? https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html Specify Vector3.forward as the axis. and, angle is the angleDelta since the last frame.

or, transform.rotation = Quaternion.Euler(0,0,zAngle);

Comment
Add comment · Share
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

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

146 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

Related Questions

Freeze rotation but needs angular rotate token in Rigidbody 2D 0 Answers

Local scale flip is breaking rotation 1 Answer

Spin a wheel realistically like spinning a Wheel of Fortune. 3 Answers

Locking and Limiting a rotation 2d 0 Answers

Trying to move Rigidbody2D towards player. Works fine when player is standing still, but once player starts moving, it flips out. 0 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