- Home /
Rotating Sutff
So, I have a script that rotates based on mouse input. How do I make it so it affects the two gameobject variables in the script rather than the object that the script is attached to?
Answer by Eno-Khaon · May 05, 2018 at 02:44 AM
Rather than just affecting your current object's Transform, you can simply affect other ones instead through the GameObject variables.
// C#
public GameObject otherObj1;
public GameObject otherObj2;
// ...
// Replace something along the lines of...
transform.rotation = rotationFromMouse;
// ... with something like:
otherObj1.transform.rotation = rotationFromMouse;
otherObj2.transform.rotation = rotationFromMouse;
@$$anonymous$$o-$$anonymous$$haon thanks, do you know how to move the camera using the mouse? (Just started unity a few months ago)
Well, you can get the main camera in a scene using Camera.main, so you can get its transform data from there.
// C#
private Transform camTransform;
private void Start()
{
camTransform = Camera.main.transform;
}
From there, modifying its position would be done using the mouse input:
// simple example, moving camera based on its orientation
public float mouseX$$anonymous$$ultiplier;
public float mouseY$$anonymous$$ultiplier;
// ...
private void Update()
{
Vector2 mouseInput = new Vector2(Input.GetAxis("$$anonymous$$ouse X") * mouseX$$anonymous$$ultiplier, Input.GetAxis("$$anonymous$$ouse Y") * mouseY$$anonymous$$ultiplier);
camTransform.position += camTransform.right * mouseInput.x;
camTransform.position += camTransform.up * mouseInput.y;
}
Edit: Whoops, forgot to actually apply the mouseInput the way I intended in my example.
@$$anonymous$$o-$$anonymous$$haon I made a mistake, I meant to rotate the camera, not move it. $$anonymous$$y bad. (For an example of what I want, look at the game $$anonymous$$inecraft)
Your answer
![](https://koobas.hobune.stream/wayback/20220612162039im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clamping Camera doesn't work 1 Answer
setting up different camera views for clients in unity network 0 Answers
Smooth camera script looking at left hand side of character. 0 Answers
Confining Mouse Look on X axis 0 Answers
Multiple Targets Camera 1 Answer