LookAt to control view angle stuck at 180 degrees rotation

I´m trying to get a control scheme working for a 2D twin stick shooter on PC with an XBox controller, i.e. movement with left stick, aiming and looking at rotation with right stick and firing in looking at direction with a separate button. View is orthographic and looking down on player from the z-axis, so movement is on x and y.

The movement and shooting bit is fine, but the problem comes with the looking at rotation with right stick. This is the current code to control the right stick:

var rh : float = Input.GetAxis("RightAnalog_horizontal") * Time.deltaTime;
var rv : float = Input.GetAxis("RightAnalog_vertical") * Time.deltaTime;
   	 
   transform.LookAt(transform.position + Vector3 (rh, rv, 0), Vector3.up); 

(which I picked up from here)

However, this results in the degree of rotation being stuck on 180 degrees, as in the following image with the green lines representing the viewing rotation:

5622-ss1.png

While of course it needs to be a free 360 degrees rotation. When it hits either extremes of this range (0 or 180 degrees, the red dots) it flips to the opposite extreme. Somewhere that transform.LookAt line isn’t working for me, but I can’t figure out for the life of me on how to fix it. I have looked at similar questions and threads, but I couldn’t extract an answer from those unfortunately.

Perhaps the problem lies with the shooting script which aligns to the viewing rotation. Maybe it messes it up in some way, so here is that bit as well:

var bullet : GameObject;
var fireRate : float = .08;
var nextShot : float = 0.0;

function LateUpdate() {
    	if (Input.GetButton("Fire1")) {
    		Shoot();
    	}
    }
    
function Shoot() {
    	if(Time.time >= nextShot){
    	var newBullet : GameObject = Instantiate(bullet, transform.position + transform.up*1.2, transform.rotation);
    	nextShot = Time.time + fireRate;
    	Physics.IgnoreCollision(newBullet.collider, collider);			
    	}
    }

I hope that you can help and thank you so much for your time!

Ps. Note that I’m not that experienced with Unity and JavaScript (as you probably already figured out, I’m just a hobbyist taking some spare time to do this). If you could take that into regards when suggesting a solution it´d be much appreciated!

First thing to come to mind would be that you might have switched one of the axes, in LookAt (I know this would be a pit fall that I could have fell in). Give it a try if you haven’t already.

//Vector3 (rh, rv, 0) is x,y,z. so I'm thinking
transform.LookAt(transform.position + Vector3 (rh, 0, rv), Vector3.up);

I hope this is what you need.

Enjoy.