I have 2D game (unity, c# scripts),
where is a submarine with tower (parent) and gun (child).
look at the screenshot
Tower can rotate (when I press A or D) around submarine body. (works fine)
Problem is the gun on tower should rotate towards mouse, but only in angle limitation. Angle limitation (by parent) now works fine, but gun doesn't look towards mouse.
void Update() {
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector2 direction = new Vector2(mousePosition.x -
transform.localPosition.x,mousePosition.y - transform.localPosition.y);
float angle = (Mathf.Atan2 (direction.y , direction.x) * Mathf.Rad2Deg);
transform.localRotation = Quaternion.Euler (new Vector3(0, 0, Mathf.Min(
Mathf.Max( Mathf.Abs(angle),40 ),140 )));
}
Here is a video where you can see how it "works" now: https://youtu.be/1pm54cjzYxA
Thanks for help!
Consider Using LookAt(); function
Related
I'm making a 2D shooter game where the player's arm should be able to pivot based on your mouse position. I have a "joint" object set to the shoulder of the arm for it to rotate around. Everything I've searched pointed me to transform.RotateAround, so I used that, but the problem is the arm doesn't FACE the mouse, it just continuously rotates based on the angle of the mouse. I know of transform.rotation, but I don't believe you can rotate around a point like that. Here's what I currently have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateArm : MonoBehaviour
{
public GameObject joint;
public Camera cam;
private Vector3 mousePosition;
private Vector3 lookAtPosition;
void Update()
{
mousePosition = Input.mousePosition;
lookAtPosition = cam.ScreenToWorldPoint(mousePosition);
var angle = Vector2.Angle(lookAtPosition, joint.transform.position);
transform.RotateAround(joint.transform.position, Vector3.forward, angle);
}
}
Calculate the angle between the arm's current forward direction and the direction towards the mouse, then use that angle to set the arm's rotation.
void Update()
{
mousePosition = Input.mousePosition;
lookAtPosition = cam.ScreenToWorldPoint(mousePosition);
Vector3 direction = lookAtPosition - joint.transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = rotation;
}
I found the solution to my problem. All I had to do was add the arm as a child object of the joint. I attached my rotate script to the joint, and made the joint rotate toward the mouse. This made the arm rotate perfectly around the joint.
I am trying to make my character point to where my mouse cursor is, however this isn't functioning properly. I've attached my script "test_1" to the character and makes it turn which is amazing, but it needs to face my mouse position. It's currently just randomly rotating as I move my mouse around the character. I tried attaching the script to the main camera hoping it works, but the camera just flips around in a circle constantly, so the script is currently attached to the player. I'm just looking for a way to rotate and point my character to the mouse position while looking at an angle with the main camera(player view). Here is the video. A good game reference would be Babo Violent 2.
My code:
using UnityEngine;
public class test_1 : MonoBehaviour
{
void Update()
{
Vector3 vecWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);
float fAngle = AngleBetweenPoints(transform.position, vecWorldPos);
transform.rotation = Quaternion.Euler(new Vector3(0f, fAngle, 0f));
}
float AngleBetweenPoints(Vector2 a, Vector2 b)
{
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
}
}
Thanks in advance!
Try adding a -90 degrees or a 90 degrees offset, maybe this might be the issue.
I've been doing some research on why my player(GameObject) is does not rotate toward my mouse position in my TopDown 3D game and I can't seem to find what is wrong with my code, so im making this post. The problem thats I have is that only the GameObject of my player (in my case, a capsule) rotate toward my mouse position but the axis of my player stays the same. In other word, I can't rotate the axis of my player, to face my mouse position, but I can rotate the GameObject of my player to face my mouse position. Its really hard to explain and this never happened to me before. Question is how can I rotate the axis of my player to face my mouse position. Keep in mind that my game is a top down view.
Here is the code im using for my playerMouvment and for my mouseLook:
public class Controller : MonoBehaviour
{
public float moveSpeed = 6;
Rigidbody rb;
Camera viewCamera;
Vector3 velocity;
void Start()
{
rb = GetComponent<Rigidbody>();
viewCamera = Camera.main;
}
void Update()
{
Vector3 mousePos = viewCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, viewCamera.transform.position.y));
transform.LookAt(mousePos + Vector3.up * transform.position.y);
velocity = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized * moveSpeed;
}
void FixedUpdate()
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
}
Again I tried and look for any error in my code and I can't find anything that cause this weird situation and so if anyone can help me find a better way to write this code and solve my problem it would be great!
If I understand you correctly everything is fine with your game and the player is turning as it should, but only in the editor that the axis of the player (red, green and blue arrows) don't turn with the player?
If this is the problem it might be that you are using global space handle instead of local space. Clicking the icon I highlighted in the image should do the trick.
Use this code instead of LookAt() function
Vector2 direction = mousePos.position - player.transform.position;
player.transform.right /* Maybe you need Up or -Up or -right */ = direction;
This would work too in some cases
Vector2 direction = new Vector2
(
mousePos.position.x - player.transform.position.x,
mousePos.positoin.y - player.transform.position.y
)
player.transform.right /* Maybe you need Up or -Up or -right */ = direction;
I'm trying to get a sprite following my mouse position with my camera rotated at 30 on x axys, this works fine if camera have a rotation of 0,0,0 but not on 30,0,0, how I have to calculate this? I have tryed substracting to x position with no success, here is my code:
this is attached on the object I want to follow the mouse
private void FixedUpdate()
{
Vector3 pos = cam.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(pos.x, pos.y, transform.position.z);
}
EDIT: also my camera is ortographic not perspective
ScreenToWorldPoint isn't really appropriate here because you don't already know the proper distance to put the sprite away from the camera. Instead, consider using a raycast (algebraically, using Plane) to figure where to put the sprite.
Create an XY plane at the sprite's position:
Plane spritePlane = new Plane(Vector3.forward, transform.position);
Create a ray from the cursor's position using Camera.ScreenPointToRay:
Ray cursorRay = cam.ScreenPointToRay(Input.mousePosition);
Find where that ray intersects the plane and put the sprite there:
float rayDist;
spritePlane.Raycast(cursorRay, out rayDist);
transform.position = cursorRay.GetPoint(rayDist);
Altogether:
private void FixedUpdate()
{
Plane spritePlane = new Plane(Vector3.forward, transform.position);
Ray cursorRay = cam.ScreenPointToRay(Input.mousePosition);
float rayDist;
spritePlane.Raycast(cursorRay, out rayDist);
transform.position = cursorRay.GetPoint(rayDist);
}
I have a problem.
I want to use joystick to movement player.
I want to rotate camera if player click ANYWHERE ELSE on screen.
Here is picture
My problem:
If the player use joystick then camera rotate too!
I tried IsPointerOverGameObject not good, because player press the joystick and drag it to the screen then joystick still work, but camera rotate again :(
So I think if player use joystick then I disable rotate camera, but not good, because If player use two fingers it is possible both at the same time. (one finger joystick and one finger screen)
public float speed = 2.0f;
private float X;
private float Y;
void Update() {
if (Input.GetMouseButton (0)) {
if(!EventSystem.current.IsPointerOverGameObject ()) {
transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * speed, -Input.GetAxis ("Mouse X") * speed, 0));
X = transform.rotation.eulerAngles.x;
Y = transform.rotation.eulerAngles.y;
transform.rotation = Quaternion.Euler (X, Y, 0);
}
}
}
So another solution is needed.
If player use the joystick, do not move the camera.
But if player use it with two fingers (one with the joystick and the other with the camera allowed!)
I hope you understand. (similar to other shooting games).
Thanks!