I have a player at the bottom of the screen, and an enemy at the top of the screen. I want them both to continuously rotate towards each other. Here is my code for both:
void Update ()
{
Vector3 diff = target.position - transform.position;
diff.Normalize();
float zRotation = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
Vector3 lookDirection = new Vector3(0, 0, zRotation - 90);
transform.rotation = Quaternion.Euler(lookDirection);
}
Given this state, the player behaves as expected, but the enemy always rotates away from the player, meaning that he looks up instead of down at where the player is.
If I switched the diff vector like this:
Vector3 diff = transform.position - target.position;
Then it's the player that looks away from the enemy.
Both sprites have 0 values for rotation and scale in the editor.
What is wrong here?
Your solution works fine for me: http://imgur.com/a/spi3O
Could it be that the "up" for the Enemy is the "down" for the player. Meaning one of your sprites is drawn upside down?
You can try the following, but it yields the same result:
var dir = target.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
If you're going to use the same method of calculating the rotation, you need to assure that your sprites have the same world-relative native rotation, or fix that.
Related
I have an object that rotates on the y axis with the "Mouse X" input and a video camera that through a slerp quaternion should follow the rotation of the object.
Unfortunately, when I rotate the object with fast mouse movements, the rotation of the slerp camera stops as if it hits a wall and does not follow that of the object.
Without quaternion slerp it works following the object but I need to do it with the interpolation.
rotation object (in LateUpdate:
float rot = Input.GetAxisRaw("Mouse X") * speedrotation;
transform.Rotate(0, rot, 0);
rotation camera (in LateUpdate):
var rot = Quaternion.Slerp(camera.transform.rotation, object.rotation, speed_rot * Time.deltaTime);
camera.transform.position = object.transform.position + rot;
camera.transform.LookAt(object.transform.position);
Try EulerAngles instead! That is better option for make camera controller!
Simple Code:
Vector3 currentCameraEulers;
float rotationDPI = 7f; // I didn't remeber how to call better this thing
void Update(){
float cameraX = Input.GetAxisRaw("Mouse X") * rotationDPI;
if(cameraX != 0f){
currentCameraEulers = new Vector3(0, cameraX, 0);
transform.eulerAngles += currentCameraEulers;
}
}
That should work completely fine.
I am making a Top-Down 2D shooting game and i did spot a trouble.
The point is, player gun has weird rotation whenever the player rotate.
I made my Player face mouse position. Player gun is not in the center of sprite.
The Gun is a prefab in PlayerHand and PlayerHand is a Child of Player.
I tried a lot of things and yet still i cant find a solution.
public class HandHolder : MonoBehaviour
{
[SerializeField] Gun gun;
[SerializeField] float offsetX;
[SerializeField] float offsetY = 0.01f;
Gun playerGun;
void Awake ()
{
playerGun = Instantiate(gun,transform.localPosition,transform.localRotation) as Gun;
}
// Update is called once per frame
void Update ()
{
playerGun.transform.position = new Vector3(transform.position.x + offsetX,transform.position.y + offsetY);
playerGun.transform.rotation = transform.rotation;
playerGun.Shooting();
}
}
void Update()
{
if (!isLocalPlayer)
return;
Vector3 position = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized * Time.deltaTime * 20f;
transform.position += position;
FaceMouse();
}
public void FaceMouse()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
mousePosition.Normalize();
float rotation_z = Mathf.Atan2(mousePosition.y, mousePosition.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotation_z);
}
Here are the screenshoots. My Player already has a gun in texture. but it is only texture. I want the Gun prefab to be exactly on the place of Player sprite gun, whenever i rotate.
add an empty to your player, name it gunTransform. tag it GunTransform make sure the forward axis(blue) is facing the players forward direction.
Class Level variable -
Transform guntransform;
in Awake():
guntransform=this.GameObject.FindObjectWithTag("GunTransform").getComponent<Transform>();
then instead of
playerGun = Instantiate(gun, transform.localPosition, transform.localRotation) as Gun;
call
playerGun = Instantiate(gun, guntransform.position, guntransform.rotation) as Gun;
enemy not looking at the player standing at height my enemy does not look in the upward direction while shooting when the player is standing at the height i used these two methods but none of them making the enemy look towards the player when the player is at some height , im also adding the picture to make it clear
First method :
transform.LookAt (ThePlayer.transform.position);
Seond method:
Vector3 direction = ThePlayer.transform.position - transform.position;
direction.y = 0;
if (direction.x != 0 && direction.z != 0) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation
(direction), 1.5f * Time.deltaTime);
transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
Yeah on Draco18s's post, you should try moving the enemy's head with Tranform.LookAt(myplayer.trasform.position) however note that the enemy will look at the player's pivot point, so if the player's pivot point is at their feet, that is where the enemy will look.
Best of luck.
I have two sprites and I am rotating them on mouse drag. When I rotate one, another has to rotate also but a bit slower. The issue is when I move first sprite too fast. I guess Unity skips some points where I ask for current rotation of object. Here is the code:
if(objekatKliknut=="Minute"){
mouseClickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = mouseClickPos - transform.position;
angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
angle-=90;
if (angle < 0.0f) angle += 360.0f;
angle = Mathf.Round(angle/6.0f)*6.0f;
if(angle==360) angle=0;
hand1.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
a1=angle;
if(a1!=a2){
float x = Mathf.Abs(a1-a2);
if(a1>a2){
moveRight = false;
if(x==6){
handRot+=addAngle*x/6f;
globeRot-=addGlobeAngle;
}
a2=a1;
}
else if(a1<a2){
moveRight = true;
if(x==6){
handRot-=addAngle*(x/6f);
globeRot+=addGlobeAngle*(x/6f);
}
a2=a1;
}
}
hand.transform.rotation = Quaternion.AngleAxis(handRot, Vector3.forward);
oblaci.transform.rotation = Quaternion.AngleAxis(globeRot, Vector3.forward);
}
hand1 is first object that I am rotating, and hand is the second one that needs to be rotated relative to the first one.
Can someone please help me?
Is there a way to limit the transform of a game object on the x axis? I am using GUI Textures as buttons to slide a game object right and left, but I don't want to it to slide outside of the screen view. Any advice is appreciated.
public float moveSpeed = 0.5f;
void Update () {
float horiz = Input.GetAxis ("Horizontal");
transform.Translate(new Vector3(horiz * moveSpeed,0,0));
}
You can use the Mathf.clamp() method.
Example:
float xPos = Mathf.clamp(xPos, minX, maxX);
This makes sure that if xPos goes below minX it will be set to minX and if it goes above maxX it will be set to maxX meaning that xPos can never be higher than maxX or lower than minX.
A simple way is to put game objects (ex. cube) as boundary of the screen. That way, you cant move your game object outside the screen, because it will collide with the boundary and stop going outside the screen.