- Home /
Making an object rotate towards touch
So basically what I am trying to do is make a 2D mobile platformer, controlled with a joystick. I want the player to be able to shoot where he touches while ignoring the buttons and Joystick. I tried for several hours now and now I am out of ideas. UPDATE: I added a Button for shooting that covers the screen, excluding the other button areas. When I test it, it is not accurate and seems to be off sometimes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerShooting : MonoBehaviour
{
public float offset;
public GameObject projectile;
public Transform shotPoint;
private float timebtwshots;
public float startTimeBtwShots;
void Update()
{
if (timebtwshots <= 0)
{
if (CrossPlatformInputManager.GetButtonDown("Shoot"))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
timebtwshots = startTimeBtwShots;
Vector3 diffecence = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) - transform.position;
float rotZ = Mathf.Atan2(diffecence.y, diffecence.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
}
}
else
{
timebtwshots -= Time.deltaTime;
}
}
}
I am using Unity standard assets and separate Joystick assets. Unity version 2020.3.11f1
This is the script for the instantiated projectile:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectilePlayer : MonoBehaviour
{
public float speed;
public float duration;
void Start()
{
Invoke("DestroyProjectile", duration);
}
void Update()
{
transform.Translate(transform.up * speed * Time.deltaTime);
}
void DestroyProjektile()
{
Destroy(gameObject);
}
}
Have you tried using Input.GetTouch() instead of Input.GetMousePosition?
Yes I tried using this but it did not change much
@Schnekorino I mean you can try using it like this:
Touch lastTouch;
if(Input.touchCount > 1)
{
lastTouch = Input.touches[Input.touchCount - 1];
}
I think a solution could be to somehow exclude the Joystick area from touch detection (I already tried blocking UI touches, but it still rotated towards it, when it was touched
Answer by Tinglee82 · Jun 21, 2021 at 01:05 PM
You can try this:
if (CrossPlatformInputManager.GetButtonDown("Shoot"))
{
Instantiate(projectile, shotPoint.position, transform.rotation);
timebtwshots = startTimeBtwShots;
// Vector3 diffecence = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) - transform.position;
// float rotZ = Mathf.Atan2(diffecence.y, diffecence.x) * Mathf.Rad2Deg;
// transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
// I'm assuming transform.forward is parallel to the direction that your player's "gun" is pointing. Otherwise you can also use transform.right or something.
var _gunForward = transform.forward
var _newForward = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position) - transform.position;
// we'd wanna to zero the z-direction since you're working on a 2d game
_newForward = 0;
// you can use quaternion multiplication to chain multiple rotations (sequence matters)
transform.rotation *= Quaternion.FromToRotation(_gunForward, _newForward);
}
Answer by eneIr · Jun 26, 2021 at 09:19 AM
@Schnekorino I mean you can try using it like this:
Touch lastTouch;
if(Input.touchCount > 1)
{
lastTouch = Input.touches[Input.touchCount - 1];
}
And then use the position of lastTouch for your rotation.
Answer by spilat12 · Jun 26, 2021 at 05:48 PM
The approach I am using for this in my mobile games: have an empty game object as "aim". Whenever you press the screen, its location changes to the location of the touch. The character, however, is looking at the "aim" - that would be a simple LookAt() function! I really like the result, maybe it would suit you as well!
Your answer
Follow this Question
Related Questions
How to make UI Button Fire Continuously ? 3 Answers
Shoot when GUI is pressed? 0 Answers