- Home /
How can i rotate only Z axis of Player.
Hi! I am making a game like "diep.io" for mobile. I made a movement system with using Joystick system and i want to do it on shooting system,too. I tried but i couldn't. I want make a shooting system with joystick system. I guess i have to rotate the Z axis of player with joystick but i don't know how can i do it. So i need help.
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 20f;
void Update()
{
if (Input.GetButtonDown("Fire2"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right);
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
if (hitInfo)
{
Enemy enemy = hitInfo.transform.GetComponent<Enemy>();
}
}
}
Rotating is always done around axis.. i saw the game you mentioned .. So You CanUse :
public void LookAt(Transform target);
Take look at it here https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
Sure! In the game you shared the player is rotating to mouse position so give this a try:
void Update()
{
Vector3 mouseposition= Input.$$anonymous$$ousePosition;
mouseposition=Camera.$$anonymous$$ain.ScreenToWordPoint(mouseposition);
Vector2 DirectionTo$$anonymous$$ousePos=new Vector2(mouseposition.x-
transform.position.x,mouseposition.y-transform.position.y);
transform.up=DirectionTo$$anonymous$$ousePos; //Which axis you want to face
mousepos
}
Thx but i know it. Just i want do it with joystick, not mouse. I am making a mobile game so i can't use a mouse. But i don't know how can i do it with joystick. Can you help me? public class ShootingJoystick : $$anonymous$$onoBehaviour { public Rigidbody2D rb; public Camera cam; Vector2 movement; Vector2 mousePos;
private Vector2 moveVelocity;
public Transform player;
private bool touchStart = false;
private Vector2 pointA;
private Vector2 pointB;
public Transform circle2;
public Transform outerCircle2;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
Vector3 mouseposition = Input.mousePosition;
mouseposition = cam.ScreenToWorldPoint(mouseposition);
Vector2 DirectionTo$$anonymous$$ousePos = new Vector2(mouseposition.x -
transform.position.x, mouseposition.y - transform.position.y);
transform.up = DirectionTo$$anonymous$$ousePos; //Which axis you want to face
if (Input.Get$$anonymous$$ouseButtonDown(1))
{
pointA = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
circle2.transform.position = pointA * +1;
outerCircle2.transform.position = pointA * +1;
circle2.GetComponent<SpriteRenderer>().enabled = true;
outerCircle2.GetComponent<SpriteRenderer>().enabled = true;
}
if (Input.Get$$anonymous$$ouseButton(1))
{
touchStart = true;
pointB = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z));
}
else
{
touchStart = false;
}
}
void FixedUpdate()
{
if (touchStart)
{
Vector2 offset = pointB - pointA;
Vector2 direction = Vector2.Clamp$$anonymous$$agnitude(offset, 1.5f);
circle2.transform.position = new Vector2(pointA.x + direction.x, pointA.y + direction.y) * +1;
}
else
{
circle2.GetComponent<SpriteRenderer>().enabled = false;
outerCircle2.GetComponent<SpriteRenderer>().enabled = false;
}
}
}
Answer by Masamone · Dec 11, 2019 at 03:25 AM
I still need help, can someone help me??
Sorry The Title Was "How can i rotate only Z axis of Player" and was thinking about how to implement rotation.. anyway my bad
Did You Settup Your Own Axis? give this a try:
void Update()
{
float HorizontalInput = Input.GetAxis( "Horizontal" ); //Get The Horizontal Axis(x) Of
joystick
transform.eulerAngles+=new Vector3( 0 , 0 , HorizontalInput ); //add rotation
}
This time the player's z-axis is changing automatically. I can't control it. I have a ready joystick system but i don't know how can i control the z axis of player with the joystick. I have been stuck in this problem, im trying to solve the problem but i can't. Please help me.
what do you mean by automatically? i test it and the result is: https://gfycat.com/delayedwellmadeabyssiniangroundhornbill with no problem? do you use standard unity joystick? if you did take look here you should match your joystick with the input system https://www.youtube.com/watch?v=VW58CT27Ls$$anonymous$$