I wish to rotate an object with the mouse but also take the camera rotation into account. At the moment the object has the same rotation as the camera with the following code:
public class ObjectRotator : MonoBehaviour {
public Transform cam;
void Start () {
cam = Camera.main.transform;
}
void FixedUpdate () {
transform.rotation = cam.rotation;
}
}
The code allows the object to rotate according to my camera view, which makes it “follow” my tilt of the camera. I also want to rotate the object sideways with the mouse, along the “x axis” (which could be any axis really since the camera can be rotated however). The camera has a fixed position which makes the mouse function in a “2D environment”.
I’ve attached two images below to try and explain what I mean. The first show the scene view and the other the game.
So as you can see I have my object that rotates with the camera. I want the object to maintain this rotation with the camera (mainly to push/rotate the object from the camera, it has to change when rotating sideways obviously) but I also want it to be able to rotate to the left and to the right as shown in the second image. I hope this clears it up.
The object I wish to rotate is supposed to be a missile controlled by the mouse. So the game is 3D but the view is 2D. So since my camera is tilted I need the object to be tilted as well otherwise it’s going to look weird, and then I just want the missile to rotate to the left/right depending on where my mouse is.
I played around a bit with LookAt and it seems to work but I am unsure how myUpDirection works. Should this be a vector pointing to the mouse perhaps?
If you’re using a rigidbody for the missile, it’s not a good idea to use transform at all to make rotation changes, as this will mess with the physics.
Is your camera following the missile, or vice versa? It seems like a good idea to control the missile directly, and make the camera follow along.
Stationary relative to the missile? Why not just put the camera at missilePosition + offset, use Transform.LookAt to make it look at the missile, and specify the upDirection according to whichever direction the camera’s local up axis should use?
Hope this is helpful, unfortunately I’m not really able to come up with anything else based on the information.
I have watched this thread a little bit, quietly. I do not completely understand your goal.
I have a few questions, some might be silly, but just checking:
do you want to rotate around some space, or just rotate itself?
does the camera change its tilt at all, and the object should update, or it’s tilted always (once) and that affects the object?
is your goal to get the object (aka missile ) to turn left/right with the mouse (while continuing forward on its own axis)?
anything else you may want to add that could be helpful lol
By space, how do you mean? I want to rotate the missile, that’s all.
No, the tilt of the camera is fixed while playing. However I should be able to change the camera tilt/position when setting up the game itself/changing location in the game but it’s not going to change during gameplay.
The goal is to make it turn left and right with the mouse position, yes. The final goal is to make it intercept another missile which is fired from a location (the map is of North and South Korea). The missile which should be intercepted is moving in a 3D environment (everything is really) but the intercepting missile is meant to be done in 2D with raycasting from the missile which is guided by the player. I have not really figured out exactly how the game mechanics are going to be yet since I’m not that far. At the moment I just want to control the missile with the mouse at the same time as it’s rotating away from the camera so it won’t look weird.
That seems to work so far, thanks! Now I’m trying to make the object follow my mouse, to make sure it really works, but I’m having trouble with this as well… All I want really is the object to be at the x and y position of the mouse in the game view, using MoveTowards or similar. I found this but it doesn’t really do what I want, it just moves the object to the camera.
Thanks for the help (and from Billy as well!). I didn’t change anything to make it work. However… I’ve realized that’s not what I needed Now I need the missile to follow my mouse position in the game view, by changing rotation and position. Like this:
Image
I tried with the following code but the object wobbles:
void Start()
{
p = new Vector3();
c = Camera.main;
mousePos = new Vector2();
}
void Update()
{
mousePos.x = Input.mousePosition.x;
mousePos.y = Input.mousePosition.y;
p = c.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, c.nearClipPlane + (float)0.3));
transform.position = Vector3.Lerp(transform.position, p, Time.deltaTime * moveSpeed);
transform.position = new Vector3(transform.position.x, 0.15f, transform.position.z);
//transform.rotation = new Quaternion(0, transform.rotation.y, transform.rotation.z, transform.rotation.w);
//transform.rotation = Quaternion.LookRotation(p, transform.up);
}
I also attempted locking the angle of the object to prevent this but without success (last two lines). The object is fixed at 0.15f on the y-axis so it won’t collide with the ground, and also “fakes” a floating effect in “2D”.
Another problem I encountered while doing this was that the object never reached the actual mouse position at some points, so I had to drag the mouse further to get it where I wanted. I believe this has something to do with the transition of coordinate systems/the camera.
Well, not sure how much progress we’re getting here lol
To be honest, I mostly understand what you’re looking for, but who knows.
I tried 1 more test with this… This is something I had been working on before you posted that you solved it. I modified it slightly so the capsule is pointing a different direction, that’s it.
Rigidbody rb;
public float rotSpeed = 4;
public float moveSpeed = 2;
Quaternion targetRot;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 spos = Camera.main.ScreenToWorldPoint(
new Vector3(Input.mousePosition.x, Input.mousePosition.y,
Mathf.Abs(transform.position.z - Camera.main.transform.position.z)));
Vector3 dir = spos - transform.position;
dir.Normalize(); // not really sure if this is needed.
// You may not need the (90,0,0) if your object is already rotated. I was testing with a capsule. :)
targetRot = Quaternion.Euler(90,0,0) * Quaternion.FromToRotation(Vector3.up, dir);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, rotSpeed * Time.deltaTime);
}
private void FixedUpdate()
{
rb.velocity = transform.up * moveSpeed;
}
It can’t go up or down, it can only turn (and of course, just flies forward based on its new forward pointing… which, for the capsule in my test was up, but it’s laying down… if that makes sense).
lol
I think you misunderstood what I meant. Anyways I managed to sort of do what I wanted except for one thing. The capsule follows my mouse and it rotates, but it’s rotating on the wrong axis. I did solve this by just changing the width/height of the capsule to “fake” it looking like a capsule in the correct direction. Anyways, here’s the code for anyone finding this thread later:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewRotate : MonoBehaviour {
public float speed = 10f;
public float moveSpeed = 0.01f;
Plane ground;
void Update()
{
RotateToMousePosition();
}
void RotateToMousePosition()
{
ground = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//*distance only needs an initial value to work
float dist = 0f;
if (ground.Raycast(ray, out dist))
{
//create a vector position at latest raycast hit point
Vector3 mousePoint = new Vector3(ray.GetPoint(dist).x,
transform.position.y, ray.GetPoint(dist).z);
//*useful for showing where ray comes from in testing ONLY
Debug.DrawLine(ray.origin, mousePoint);
//rotate object toward vector position
Quaternion targetRotation = Quaternion.LookRotation(mousePoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
//transform.rotation = new Quaternion(90, transform.rotation.z, transform.rotation.z, transform.rotation.w);
transform.position = Vector3.Slerp(transform.position, mousePoint, moveSpeed * Time.deltaTime);
}
}
}