- Home /
Rotating object to face mouse with physics isn't working
(First time on the forums) So, I want my players ship to face the mouse using physics and it sort of does... I have posted a screenshot below to illustrate the problem
My code looks as follows:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public GameObject thruster;
public float thrust;
public float torque;
private float optimalDirection;
public float mouseAngle;
private float playerRotation;
float angle;
void Start() {
rigidbody2D.centerOfMass = new Vector2(0.0f,0.0f);
}
void Update() {
}
// Update is called once per frame
void FixedUpdate()
{
//Enables acceleration
if ((Input.GetAxisRaw("Vertical") > 0) && (Input.GetKey("left shift")))
{
rigidbody2D.AddForce((transform.up * Time.deltaTime * thrust) * 1.5f, ForceMode2D.Force);
thruster.particleEmitter.emit = true;
}
else if (Input.GetAxisRaw("Vertical") > 0)
{
rigidbody2D.AddForce(transform.up * Time.deltaTime * thrust, ForceMode2D.Force);
thruster.particleEmitter.emit = true;
}
else
{
thruster.particleEmitter.emit = false;
}
//Allows ship to strave
rigidbody2D.AddForce(transform.right * Time.deltaTime * (thrust / 2) * Input.GetAxisRaw("Horizontal"), ForceMode2D.Force);
//Mouse rotation
Vector2 mouse = Camera.main.ScreenToViewportPoint(Input.mousePosition); //Mouse position
Vector3 objpos = Camera.main.WorldToViewportPoint(transform.position); //Object position on screen
Vector2 relobjpos = new Vector2(objpos.x - 0.5f, objpos.y - 0.5f); //Set coordinates relative to object
Vector2 relmousepos = new Vector2(mouse.x - 0.5f, mouse.y - 0.5f) - relobjpos;
mouseAngle = Vector2.Angle (Vector2.up, relmousepos); //Angle calculation
if (relmousepos.x > 0)
mouseAngle = 360-mouseAngle;
//sets 2 variables to aid with figuring out which direction to go
playerRotation = rigidbody2D.rotation;
//Finds the optimal direction
optimalDirection = ((((mouseAngle - (playerRotation)) % 360) + 540) % 360) - 180;
//Makes ships come to a stand still
if (Mathf.Round(optimalDirection) == 0)
rigidbody2D.rotation += optimalDirection;
//Chages velocity when appropriate
if (Mathf.Round(optimalDirection) > 0 && rigidbody2D.angularVelocity < -5)
rigidbody2D.angularVelocity = rigidbody2D.angularVelocity / 1.5f;
if (Mathf.Round(optimalDirection) < 0 && rigidbody2D.angularVelocity > 5)
rigidbody2D.angularVelocity = rigidbody2D.angularVelocity / 1.5f;
//Applying torque relative to Optimal_Direction
if (Mathf.Round(optimalDirection) > 0)
rigidbody2D.AddTorque(torque * Time.deltaTime);
if (Mathf.Round(optimalDirection) < 0)
rigidbody2D.AddTorque(-torque * Time.deltaTime);
}
}
I'm not sure what the issue is and was hoping for some sort of solution or alternative, the problem is reoccurring with every side however having the mouse at 90, 180, 270 and 360 degrees relative makes it align perfectly
Answer by carrollh · Jan 23, 2015 at 04:12 AM
EDIT: Fixed the code. This should work if you have a thing in the background with a collider on it.
I personally think you are doing waaaaay too much work. Look at Transform.LookAt You could just call it and be done. Lerp it if you want for the effects it looks like you're doing.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Physics.Raycast(ray, out hit);
if (hit.collider != null)
{
if (hit.collider.tag == Tags.enemy) {
transform.LookAt(hit.collider.transform.position);
} else transform.LookAt(hit.point);
}
I'm glad it's working, but it still looks like a lot of code.
Also I just realized my code snippet will turn the object to face a thing with the enemy tag if clicked on, otherwise it will turn to point at the point on the background you clicked on.
It is quite bulky indeed, I will probably attempt to optimize it later on Thank you for replying :)
Answer by jamiecus · Jan 23, 2015 at 03:39 PM
Transform.LookAt did not provide the desired effect however using the following worked //Finds the rotation of mouse Vector3 diff = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; diff.Normalize(); mouseAngle = (Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg)-90;
//sets 2 variables to aid with figuring out which direction to go
playerRotation = rigidbody2D.rotation;
//Finds the optimal direction
optimalDirection = ((((mouseAngle - (playerRotation)) % 360) + 540) % 360) - 180;
//Makes ships come to a stand still
if (Mathf.Round(optimalDirection) == 0)
rigidbody2D.rotation += optimalDirection;
//Chages velocity when appropriate
if (Mathf.Round(optimalDirection) > 0 && rigidbody2D.angularVelocity < -5)
rigidbody2D.angularVelocity = rigidbody2D.angularVelocity / 1.5f;
if (Mathf.Round(optimalDirection) < 0 && rigidbody2D.angularVelocity > 5)
rigidbody2D.angularVelocity = rigidbody2D.angularVelocity / 1.5f;
//Applying torque relative to Optimal_Direction
if (Mathf.Round(optimalDirection) > 0)
rigidbody2D.AddTorque(torque * Time.deltaTime);
if (Mathf.Round(optimalDirection) < 0)
rigidbody2D.AddTorque(-torque * Time.deltaTime);
I am making a school project (Space game) with unity and I need to have rotation with physics. Sadly, this script is not working for me and modifications are not working. What I have noticed is that I am getting wrong mouseAngle. Also what you wanted to get with optimalDirection? Thanks in advance.
Your answer
Follow this Question
Related Questions
Calculating the Angle between two vectors 2 Answers
Issues calculating angle in 2D 0 Answers
Finding the angle between 2 clicked points 2 Answers
Rotate so VectorY points to another Vector3 2 Answers
2D GunPivot Bug 1 Answer