Unity - problem with arms rotation and flip, body flip and shotting - 2d shooter - unity3d

I'm starting out with a small shooting game but I have a problem with my character. The arms have to rotate 360º but the body only right or left (depending on where the rotation of the arms by the mouse).
What I got so far is what you see in the video below but I have two big problems and with the help of tutorials.
I was able to rotate and flip my arms but not the body.
Also, when it fires to the right the bullets exit correctly from the firepoint that I created but after the arms flip to the left the bullets (and the weapon fire) are no longer aligned.
Is this approach that I have tried is not the best for this problem?
I appreciate your help.
Game link: https://vimeo.com/310853740
Here my arm rotation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArmRotation : MonoBehaviour
{
SpriteRenderer spriteRend;
void Awake()
{
spriteRend = GetComponent<SpriteRenderer>();
}
void Update()
{
AimArmAtMouse();
}
void AimArmAtMouse()
{
Vector2 mousePosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 armToMouse = mousePosition - (Vector2)transform.position;
float rotationZ = Vector2.SignedAngle(transform.right, armToMouse);
transform.Rotate(0f, 0f, rotationZ);
FlipArm(Vector2.SignedAngle(transform.right, Vector2.right));
}
void FlipArm(float rotation)
{
if (rotation < -90f || rotation > 90f)
{
spriteRend.flipY = true;
}
else
{
spriteRend.flipY = false;
}
}
}

It's because you don't flip the firepoint when you flip the sprite. I re-wrote you script to include a reference to the firepoint. I also added a 'FlipFirePoint' function which gets called by your 'FlipArm' function. It should fix your alignment issue.
using UnityEngine;
public class ArmRotation : MonoBehaviour
{
SpriteRenderer spriteRend;
public Transform firePoint;
void Awake()
{
spriteRend = GetComponent<SpriteRenderer>();
}
void Update()
{
AimArmAtMouse();
}
void AimArmAtMouse()
{
Vector2 mousePosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 armToMouse = mousePosition - (Vector2)transform.position;
float rotationZ = Vector2.SignedAngle(transform.right, armToMouse);
transform.Rotate(0f, 0f, rotationZ);
FlipArm(Vector2.SignedAngle(transform.right, Vector2.right));
}
void FlipArm(float rotation)
{
if (rotation < -90f || rotation > 90f)
{
spriteRend.flipY = true;
FlipFirePoint(true);
}
else
{
spriteRend.flipY = false;
FlipFirePoint(false);
}
}
void FlipFirePoint(bool flip)
{
var pos = firePoint.localPosition;
pos.x = Mathf.Abs(pos.x) * (flip ? -1 : 1);
firePoint.localPosition = pos;
}
}

#Sean, I separated the main_body from the arms and made a new script just for body rotation but now it happens to me this:
My test char
The code:
void Update()
{
Flip();
}
void Flip()
{
Vector3 theScale = transform.localScale;
Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
float WorldXPos = Camera.main.ScreenToWorldPoint(pos).x;
if (WorldXPos > gameObject.transform.position.x)
{
theScale.x = 1;
transform.localScale = theScale;
}
else
{
theScale.x = -1;
transform.localScale = theScale;
}
}}
Almost there but not yet i need😁

Related

Having two variables that both flip the same sprite renderer in Unity

I'm trying to flip a sprite in a 3D environment. It should flip depending on the camera angle (the camera can rotate) and it should also flip depending on walking direction.
I have this script which have two variables (lastPosition to track when it moves and fyRot is camera position) that I can't get to work at the same time. If I remove the other one, they both work. But if I both have them together, only the fyRot seem to flip the sprite.
Would really appreciate any help with this, I'm quite new to coding!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flippety_flippety : MonoBehaviour
{
[SerializeField] SpriteRenderer spriteRenderer;
public static bool flipX;
Vector3 lastPos;
public Transform obj; // drag the object to monitor here
public float threshold = 1.0f; // minimum displacement to recognize a
void Start()
{
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
lastPos = obj.position;
}
void Update()
{
float fYRot = Camera.main.transform.eulerAngles.y;
Vector3 offset = obj.position - lastPos;
if (offset.z > threshold)
// code to execute when Z is increased
{
// update lastPos
lastPos = obj.position;
if (flipX == false)
{
spriteRenderer.flipX = true;
Debug.Log("TestingTrue");
}
else if (flipX == true)
{
spriteRenderer.flipX = false;
Debug.Log("TestingFalse");
}
}
// code to execute when Z is getting smaller
else
if (offset.z < -threshold)
{
// update lastPos
lastPos = obj.position;
if (flipX == false)
{
spriteRenderer.flipX = true;
Debug.Log("TestingTrue2");
} else if(flipX == true)
{
spriteRenderer.flipX = false;
Debug.Log("TestingFalse2");
}
}
if (fYRot >= 180)
{
Debug.Log("TESTING");
spriteRenderer.flipX = false;
}
else if (fYRot >= -180)
{
Debug.Log("TESTING2");
spriteRenderer.flipX = true;
}
}
}

Unity - Drag with right mouse button?

I am creating a build in Unity 2019.4.12f1 and need to drag an object with Right Mouse button.
My C# skills are very limited, so far but i try.
This scripts is my attempt to be able to rotate a gameobject by holding right mouse button down and dragging ingame.
But it is wrong.
Can anyone help me correct it?´
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouseM : MonoBehaviour
{
bool dragging = false;
void Start()
{
if (Input.GetMouseButtonDown(1))
{
dragging = true;
}
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
yourOnMouseDownFunction();
dragging = true;
}
if (Input.GetMouseButtonUp(1))
{
yourOnMouseUpFunction();
dragging = false;
}
if (dragging)
{
yourOnMouseDragFunction();
}
}
}
I understood that You need to Drag Object in 3d World so Here is my Code just create new Script and attach to Object You Want to Drag it
Your Object that You need to Drag it should has a collider
using UnityEngine;
public class DragableObject : MonoBehaviour
{
private Vector3 mOffset;
private float mZCoord;
private void OnMouseDrag()
{
transform.position = GetMouseWorldPos() + mOffset;
}
private void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
mOffset = transform.position - GetMouseWorldPos();
}
private Vector3 GetMouseWorldPos()
{
Vector3 mosePoint = Input.mousePosition;
mosePoint.z = mZCoord;
var result = Camera.main.ScreenToWorldPoint(mosePoint);
return result;
}
}
you can try it by adding it to sphere or cube and if you use custom shape you should insure that the collider is in suitable size or has a meshcollier
Demo
EDIT
using UnityEngine;
public class DragableObject : MonoBehaviour
{
private bool isMouseDragging;
private Vector3 screenPosition;
private Vector3 offset;
private GameObject target;
GameObject ReturnClickedObject(out RaycastHit hit)
{
GameObject targetObject = null;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
targetObject = hit.collider.gameObject;
}
return targetObject;
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit hitInfo;
target = ReturnClickedObject(out hitInfo);
if (target != null)
{
isMouseDragging = true;
Debug.Log("our target position :" + target.transform.position);
//Here we Convert world position to screen position.
screenPosition = Camera.main.WorldToScreenPoint(target.transform.position);
offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
}
}
if (Input.GetMouseButtonUp(1))
{
isMouseDragging = false;
}
if (isMouseDragging)
{
//tracking mouse position.
Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
//convert screen position to world position with offset changes.
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;
//It will update target gameobject's current postion.
target.transform.position = currentPosition;
}
}
}
You could try the Input.GetMouseButton method, this should return a value depending on if the button is held pressed.
This one works too, if someone needs in the future.
This is for rotate though.
Sorry, i dont know why it wont let me post as code, so i post an image.
enter image description here

Trying to create a nice character controller?

I'm kind of new in the 2D environment of Unity.
I'm trying to create a platformer. For now, I have a simple map and my player.
My simple map and my player
My player have one script attached :
public class Player : MonoBehaviour
{
public float speed;
public float jump;
public GameObject raycastPoint; // Positioned at 0.01 pixel below the player
private SpriteRenderer spriteRenderer;
private Rigidbody2D body; // Gravity Scale of the Rigidbody2D = 50
private Animator animator;
private void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
body = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
private void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
if (horizontal == 1 && spriteRenderer.flipX)
{
spriteRenderer.flipX = false;
}
else if (horizontal == -1 && !spriteRenderer.flipX)
{
spriteRenderer.flipX = true;
}
body.velocity = new Vector2(horizontal * speed, body.velocity.y);
animator.SetFloat("Speed", Mathf.Abs(horizontal));
float vertical = Input.GetAxisRaw("Vertical");
if (vertical == 1)
{
RaycastHit2D hit = Physics2D.Raycast(raycastPoint.transform.position, Vector2.down, 0.01f);
if (hit.collider != null)
{
body.AddForce(new Vector2(0f, jump));
}
}
}
}
For now I have achieved the right and left movements.
For the jump, I have a child gameobject just under the player and I'm firing a raycast to the bottom so I can know if my player is grounded or not.
I have two problems.
PROBLEM NUMBER ONE.
Sometimes I feel like my "AddForce" line is executed multiple times my player is jumping really high
Problem number one image
PROBLEM NUMBER TWO.
When I'm jumping to the left or right wall, if I keep pressing the left or right key my player is not falling anymore and stay against the wall.
Problem number two image
I tried to put my code into the FixedUpdate method (I know it's better) but I had the same results.
And I tried to set the Collision Detection on Continuous but I had the same results.
Try this code for your first problem :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(BoxCollider2D))]
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(Animator))]
public class Player_Controller : MonoBehaviour {
private Rigidbody2D body;
private bool canJump, facingRight;
private Animator anim;
[SerializeField]
private float moveSpeed, jumpForce;
void Start ()
{
SetStartValues();
}
void FixedUpdate ()
{
float horizontal = Input.GetAxis("Horizontal");
animator.SetFloat("Speed", Mathf.Abs(horizontal));
Flip(horizontal);
Move(horizontal);
Jump();
}
private void SetStartValues()
{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
canJump = true;
facingRight = true;
}
private void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && canJump)
{
body.AddForce(new Vector2(0, jumpForce));
canJump = false;
}
}
private void Move(float x)
{
body.velocity = new Vector2(x * moveSpeed * Time.deltaTime, body.velocity.y);
}
private void Flip(float x)
{
if (x > 0 && !facingRight|| x < 0 && facingRight)
{
facingRight = !facingRight;
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y) ;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
canJump = true;
}
}
}
And don't forget to put the "Ground" tag on your ground object.

How to make the camera follow the character without dragging the background along with it in screen space camera render mode in Unity?

I am making a 2D platfomer and I am using screen space-camera render mode in canvas. Now the background fits inside the screen in every aspect ratio perfectly. But when I make the camera follow the character, the background also comes with it, making the character look like it is not moving.
Code for player movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D myRigidbody;
[SerializeField]
private float movementSpeed;
// Use this for initialization
void Start ()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate ()
{
float horizontal = Input.GetAxis ("Horizontal");
HandleMovement(horizontal);
}
private void HandleMovement(float horizontal)
{
myRigidbody.velocity = new Vector2 (horizontal * movementSpeed, myRigidbody.velocity.y);
}
}
Here is the camera follow code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
Vector3 velocity = Vector3.zero;
public float smoothTime = 0.15f;
public bool YMaxEnabled = false;
public float YMaxValue = 0;
public bool YMinEnabled = false;
public float YMinValue = 0;
public bool XMaxEnabled = false;
public float XMaxValue = 0;
public bool XMinEnabled = false;
public float XMinValue = 0;
void FixedUpdate()
{
Vector3 targetPos = target.position;
//vertical
if (YMinEnabled && YMaxEnabled)
{
targetPos.y = Mathf.Clamp (target.position.y, YMinValue, YMaxValue);
}
else if (YMinEnabled)
{
targetPos.y = Mathf.Clamp (target.position.y, YMinValue, target.position.y);
}
else if (YMaxEnabled)
{
targetPos.y = Mathf.Clamp (target.position.y, target.position.y, YMaxValue);
}
//horizontal
if (XMinEnabled && XMaxEnabled)
{
targetPos.x = Mathf.Clamp (target.position.x, XMinValue, XMaxValue);
}
else if (YMinEnabled)
{
targetPos.x = Mathf.Clamp (target.position.x, XMinValue, target.position.x);
}
else if (YMaxEnabled)
{
targetPos.x = Mathf.Clamp (target.position.x, target.position.x, XMaxValue);
}
targetPos.z = transform.position.z;
transform.position = Vector3.SmoothDamp (transform.position, targetPos, ref velocity, smoothTime);
}
}
If you use screen space camera, then the Canvas will move with the camera. I would suggest using Sprite Renderer instead of Canvas panel for level background. If you need to scale the Sprite according to screen, do it from the code. Also, for scrolling the background, you can follow this tutorial:
https://unity3d.com/learn/tutorials/topics/2d-game-creation/2d-scrolling-backgrounds

Jittery movement of the camera when rotating AND moving - 2D top down Unity

I am making a 2d top down game where the player can rotate the camera around himself as he moves through the world.
Everything works fine when the player is moving around the world the camera follows him fine. if he standing still then the camera rotation also works very well. However as soon as I start to do both then there is a jittery in the camera that makes all other objects jitter besides the player.
Now in working with this problem I have found out that this could have to do with the fact that I use rigidbody2d.AddRelativeForce (so physics) to move the player and that his movements are checked in FixedUpdate.
https://forum.unity3d.com/threads/camera-jitter-problem.115224/ http://answers.unity3d.com/questions/381317/camera-rotation-jitterness-lookat.html
I have tried moving my camera rotation and the following scripts to LateUpdate, FixedUpdate, Update you name it. Nothing seems to work. I am sure that there is some sort of delay between movement of the camera and the rotation that is causing this. I am wondering if anyone has any feedback?
I have tried disabling Vsync, does not remove it entirely
I have tried interpolating and extrapolating the rigidbody and although there is a difference it does not remove it entirely. Ironically it seemd if I have it set to none, it works best.
Scripts:
To Follow character, script applied to a gameobject that has the camera as a child
public class FollowPlayer : MonoBehaviour {
public Transform lookAt;
public Spawner spawner;
private Transform trans;
public float cameraRot = 3;
private bool smooth = false;
private float smoothSpeed = 30f;
private Vector3 offset = new Vector3(0, 0, -6.5f);
//test
public bool changeUpdate;
private void Start()
{
trans = GetComponent<Transform>();
}
private void FixedUpdate()
{
CameraRotation();
}
private void LateUpdate()
{
following();
}
public void following()
{
Vector3 desiredPosition = lookAt.transform.position + offset;
if (smooth)
{
transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
}
else
{
transform.position = desiredPosition;
}
}
void CameraRotation()
{
if (Input.GetKey("q"))
{
transform.Rotate(0, 0, cameraRot);
}
if (Input.GetKey("e"))
{
transform.Rotate(0, 0, -cameraRot);
}
}
public void SetTarget(string e)
{
lookAt = GameObject.Find(e).GetComponent<Transform>();
}
}
The character Controller, script applied to the Player, is called in FixedUpdate
private void HandleMovement()
{
if (Input.GetKey("w"))
{
rigid.AddRelativeForce(Vector2.up * speed);
}
if (Input.GetKey("s"))
{
rigid.AddRelativeForce(Vector2.down * speed);
}
if (Input.GetKey("a"))
{
if (facingRight)
{
Flip();
}
rigid.AddRelativeForce(Vector2.left * speed);
}
if (Input.GetKey("d"))
{
if (!facingRight)
{
Flip();
}
rigid.AddRelativeForce(new Vector2(1,0) * speed);
}
}
Try to use Coroutines. I modified some of my scripts to be more familiar to Your code, tried it, and I didn't see any jittery. I hope that will help You.
Camera Class:
public class CameraController : MonoBehaviour {
[SerializeField]
Transform CameraRotator, Player;
[SerializeField]
float rotationSpeed = 10f;
float rotation;
bool rotating = true;
void Start()
{
StartCoroutine(RotatingCamera());
}
IEnumerator RotatingCamera()
{
while (rotating)
{
rotation = Input.GetAxis("HorizontalRotation");
CameraRotator.Rotate(Vector3.up * Time.deltaTime * rotation * rotationSpeed);
CameraRotator.position = Player.position;
yield return new WaitForFixedUpdate();
}
}
private void OnDestroy()
{
StopAllCoroutines();
}
}
Player Class:
public class PlayerMovement : MonoBehaviour {
[SerializeField]
float movementSpeed = 500f;
Vector3 movementVector;
Vector3 forward;
Vector3 right;
[SerializeField]
Transform CameraRotator;
Rigidbody playerRigidbody;
float inputHorizontal, inputVertical;
void Awake()
{
playerRigidbody = GetComponent<Rigidbody>();
StartCoroutine(Moving());
}
IEnumerator Moving()
{
while (true)
{
inputHorizontal = Input.GetAxis("Horizontal");
inputVertical = Input.GetAxis("Vertical");
forward = CameraRotator.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
Vector3 right = new Vector3(forward.z, 0, -forward.x);
movementVector = inputHorizontal * right + inputVertical * forward;
movementVector = movementVector.normalized * movementSpeed * Time.deltaTime;
playerRigidbody.AddForce(movementVector);
yield return new WaitForFixedUpdate();
}
}
}
I fixed this problem finally:
First I made my cameraRotation() and follow() into 1 function and gave it a better clearer name:
public void UpdateCameraPosition()
{
if (Input.GetKey("q"))
{
transform.Rotate(0, 0, cameraRot);
}
else if (Input.GetKey("e"))
{
transform.Rotate(0, 0, -cameraRot);
}
Vector3 desiredPosition = lookAt.transform.position + offset;
transform.position = desiredPosition;
}
I then called that function from the character controller straight after the handling of movement. Both calls (handleMovement & cameraPosition) in fixedUpdate.
void FixedUpdate()
{
HandleMovement();
cameraController.UpdateCameraPosition();
}
and the jittering was gone.
So it seems it was imply because there was a slight delay between the two calls as the previous posts I read had said. But I had failed to be able to properly Set them close enough together.
Hope this helps someone.