- Home /
Question by
HemK · Sep 07, 2020 at 08:31 AM ·
rotationplayernavmeshagentscriptingbasicsjoystick
How to keep an Object looking in a direction when the Joystick Input is zero
Hi,
I'm using a joystick to rotate a character around the transform.forward axis. But when the joystick push is released, the character spins back to face down the Z axis. I'd like to keep the character facing the last direction. How can I do that? My code isn't helping me fix that issue.
public void playerLook()
{
bool canShoot = false;
Vector3 LookDir = new Vector3(shootJS.Horizontal * lookSpeed, 0f, shootJS.Vertical * lookSpeed);
Vector3 lastLook = Vector3.zero;
if (LookDir != Vector3.zero)
{
nav.transform.forward = LookDir;
lastLook = LookDir;
}
else { nav.transform.forward = lastLook;}
}
Comment
Best Answer
Answer by HemK · Sep 11, 2020 at 01:03 PM
Solved it myself this way :D
Ignore the can shoot part. That's still not working right. But Look works properly!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.AI; using Ludiq; using Bolt;
public class PlayerJSControlSc : MonoBehaviour
{
public GameObject player;
public NavMeshAgent nav;
public Text stateText;
public float moveSpeed;
public Animator animator;
public FloatingJoystick moveJS;
public FloatingJoystick shootJS;
public float lookSpeed = 10;
public int ammo;
public int mag;
public Transform shotSpawn;
public GameObject bullet;
public float reloadTime;
public Text ammoCount;
[HideInInspector]
int currentMag;
public bool canShoot;
private float shootTimer = .5f;
// Start is called before the first frame update
void Start()
{
stateText.text = "";
nav = player.GetComponent<NavMeshAgent>();
animator = player.GetComponent<Animator>();
moveJS = GameObject.Find("Floating JS_Move").GetComponent<FloatingJoystick>();
shootJS = GameObject.Find("Floating JS_Shoot").GetComponent<FloatingJoystick>();
currentMag = mag;
canShoot = false;
nav.updateRotation = false;
}
// Update is called once per frame
void Update()
{
float lookx = shootJS.Horizontal;
float looky = shootJS.Vertical;
movePlayer();
if (lookx != 0 || looky != 0)
{ playerLook(lookx, looky); canShoot = true; }
else { canShoot = false; }
if (canShoot == true)
{
shoot();
canShoot = false;
}
ammoCount.text = currentMag + "/" + ammo;
Debug.Log(currentMag);
}
public void movePlayer()
{
float x = moveJS.Horizontal;
float y = moveJS.Vertical;
nav.velocity = new Vector3(x * moveSpeed, 0, y * moveSpeed);
if (nav.velocity.x != 0 || nav.velocity.z != 0)
{ animator.SetBool("isRunning", true); }
else { animator.SetBool("isRunning", false); }
}
public void playerLook(float x, float y)
{
Vector3 LookDir = new Vector3(x * lookSpeed*Time.deltaTime, 0f, y * lookSpeed*Time.deltaTime);
Vector3 lastLook = Vector3.zero;
if (LookDir != Vector3.zero)
{
nav.transform.forward = LookDir;
lastLook = LookDir;
}
else { nav.transform.forward = lastLook; }
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613011358im_/https://answers.unity.com/themes/thub/images/avi.jpg)