So I have a script that moves my character. One joystick to move, and one to look around, what I want to do is be able to disable moving backward or to the left or right when flying. movement is a vector3 and i tried just putting “movement.x = 0;” which worked great as long as I just kept moving forward, if I turned it stopped me from going forward. The code I have below also doesn’t work as .right .left .forward and so on are only able to be read which is understandable. Is there a way to do this?
movement.right = 0;
movement.left = 0;
movement.forward * -1 = 0;
character.Move(movement);
movement = Vector3.zero;
if(Input.GetAxis(“Vertical”) > 0)
movement += transform.forward * Input.GetAxis(“Vertical”);
character.Move(movement);
This is untested code, and I’m rusty. But that should do it.
Also, you can just build the vector in local space, like how you were doing before and then use transform direction.
ie:
movement.x = 0; //Zero out left/right
movement.y = 0; //Zero out up and down
if(Input.GetAxis(“Vertical”) > 0) //If we are moving in the positive direction, forward
movement.z = Input.GetAxis(“Vertical”); //Set the z component equal to the input axis. Or just add 1 or your speed var, or however you
//want lol.
character.Move(transform.TransformDirection(movement));
Thanks for trying to help, but it’s not getting input from Input.GetAxis, rather it’s getting it from a joystick (this is for iphone). Here’s the rest of the code and the joystick code.
Player Control
@script RequireComponent( CharacterController )
var moveJoystick : Joystick;
var rotateJoystick : Joystick;
var cameraTransform : Transform;
var speed : float = 1;
var cameraPivot : Transform;
var rotationSpeed : Vector2 = Vector2(2, 1);
private var thisTransform : Transform;
private var character : CharacterController;
function Start(){ thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
}
function Update(){
var movement = cameraTransform.TransformDirection(Vector3(moveJoystick.position.x, 0, moveJoystick.position.y));
movement.Normalize();
var absJoyPos = Vector2(Mathf.Abs(moveJoystick.position.x), Mathf.Abs(moveJoystick.position.y));
absJoyPos.y = 0;
movement *= speed + ((absJoyPos.x > absJoyPos.y) ? absJoyPos.x : absJoyPos.y);
movement *= Time.deltaTime;
//movement.right = 0;
//movement.left = 0;
//movement.forward * -1 = 0;
character.Move(movement);
//FaceMovementDirection();
var camRotation = rotateJoystick.position;
camRotation.x *= rotationSpeed.x;
camRotation.y *= rotationSpeed.y * -1;
camRotation *= Time.deltaTime * 0.2;
cameraPivot.Rotate(0, camRotation.x, 0, Space.World);
cameraPivot.Rotate(camRotation.y, 0, 0);
}
Joystick
private var gui : GUITexture;
private var defaultRect : Rect;
private var guiTouchOffset : Vector2;
private var guiCenter : Vector2;
private var lastFingerId = -1;
private var tapTimeWindow : float;
static private var tapTimeDelta : float = 0.3;
static private var joysticks : Joystick[];
static private var enumeratedJoysticks : boolean = false;
public var tapCount : int;
public var position : Vector2;
public var deadZone : Vector2 = Vector2.zero;
public var normalize : boolean = false;
private var guiBoundary : Boundary = Boundary();
class Boundary {
var min : Vector2 = Vector2.zero;
var max : Vector2 = Vector2.zero;
}
function Start () {
gui = GetComponent(GUITexture);
defaultRect = gui.pixelInset;
guiTouchOffset.x = defaultRect.width * 0.5;
guiTouchOffset.y = defaultRect.height * 0.5;
guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
guiCenter.x = defaultRect.x + guiTouchOffset.x;
guiCenter.y = defaultRect.y + guiTouchOffset.y;
}
function Reset(){
gui.pixelInset = defaultRect;
lastFingerId = -1;
}
function LatchedFinger(fingerId : int){
if(lastFingerId == fingerId){
Reset();
}
}
function Disable(){
gameObject.active = false;
enumeratedJoysticks = false;
}
function Update () {
if(!enumeratedJoysticks){
joysticks = FindObjectsOfType(Joystick);
enumeratedJoysticks = true;
}
var count = Input.touchCount;
if(tapTimeWindow > 0){
tapTimeWindow -= Time.deltaTime;
}
else{
tapCount = 0;
}
if(count == 0){
Reset();
}
else{
for(var i = 0; i < count; i++){
var touch : Touch = Input.GetTouch(i);
var guiTouchPos : Vector2 = touch.position - guiTouchOffset;
if (gui.HitTest(touch.position) (lastFingerId == -1 || lastFingerId != touch.fingerId)){
lastFingerId = touch.fingerId;
if(tapTimeWindow > 0){
tapCount++;
}
else{
tapCount = 1;
tapTimeWindow = tapTimeDelta;
}
for(var j : Joystick in joysticks){
if(j != this){
j.LatchedFinger(touch.fingerId);
}
}
}
if(lastFingerId == touch.fingerId){
if(touch.tapCount > tapCount){
tapCount = touch.tapCount;
}
gui.pixelInset.x = Mathf.Clamp(guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x);
gui.pixelInset.y = Mathf.Clamp(guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y);
if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){
Reset();
}
}
}
position.x = (gui.pixelInset.x + guiTouchOffset.x - guiCenter.x);
position.y = (gui.pixelInset.y + guiTouchOffset.y - guiCenter.y);
var absoluteX = Mathf.Abs(position.x);
var absoluteY = Mathf.Abs(position.y);
if(absoluteX < deadZone.x){
position.x = 0;
}
else if(normalize){
position.x = Mathf.Sign(position.x) * (absoluteX - deadZone.x) / (1 - deadZone.x);
}
if(absoluteY < deadZone.y){
position.y = 0;
}
else if(normalize){
position.y = Mathf.Sign(position.y) * (absoluteY - deadZone.y) / (1 - deadZone.y);
}
}
}
Lol, well just replace my Input with whatever you use for input…? Same concepts…?
Do you always want it to be limited movement? Or just at certain times?
only certain times, and I don’t really understand how I would go about replacing your input with what I’m using for my input.
var limitMovement : boolean; //Add a boolean up top.
var movement = cameraTransform.TransformDirection(Vector3(moveJoy stick.position.x, 0, moveJoystick.position.y)); //Replace this line with the following if/else statement
if(limitMovement moveJoystick.position.y > 0)
var movement = cameraTransform.TransformDirection(Vector3(0, 0, moveJoystick.position.y));
else
var movement = cameraTransform.TransformDirection(Vector3(moveJoystick.position.x, 0, moveJoystick.position.y));
Try that. This should do the trick. You’ll have to control when to limitMovement though (obviously). I’m guessing you didn’t write those scripts yourself?
Lol.
No. Lol, I didn’t write them I got them from the penelope tutorial and was trying to follow the tutorial but I didn’t really understand it all. I’ll try that. Thanks!
Ok I almost got it to work using the code below. It works, so it limits it to movement forward and back, but I still need to limit it so it can only move forward. Any ideas?
if(limitMovement){
movement = cameraTransform.TransformDirection(Vector3(0,0,moveJoystick.position.x));
}
else{
movement = cameraTransform.TransformDirection(Vector3(moveJoystick.position.x, 0, moveJoystick.position.y));
}
Not sure why you have the moveJoystick.position.x in the z position first then you have it in the x position later? That will cause different movements…? But as I wrote before, add the second part to the if statement.
if(limitMovement moveJoystick.position.x > 0) //I wrote it first with the y var as that should be forward as the script indicates, but if your game has a different orientation and uses x as forward this should work. But that means in the else it should be
else{
movement = cameraTransform.TransformDirection(Vector3(moveJoystick.position.y, 0, moveJoystick.position.x));
}
I’m thinking you’re just mixing things up, if not though, that should work.
It was giving me really weird movements when i had the second part of the if statement in there, but worked fine when I took it out. I’ll try again.
Ah, yes, my bad. Add what I had, then change the else statement to
else if(!limitMovement)
Haha, my bad. What it was doing before was giving you full control normally, and then during flying(limited movement) it gave limited control forward and full control in any other direction. The else if will fix it though.
ok cool I’ll try it. Thanks.
Sweet it works perfect! Thanks! Here’s the final code I used. I don’t know how hard it would be but do you know if it would be possible to lock the joystick so it only moves forward when movement is locked? It would make the controls much easier. Thanks!
if(moveJoystick.position.x < 1){
movement = cameraTransform.TransformDirection(Vector3(0,0,0));
}
if(limitMovement moveJoystick.position.x > 0){
movement = cameraTransform.TransformDirection(Vector3(0,0,moveJoystick.position.x));
}
else if(!limitMovement){
movement = cameraTransform.TransformDirection(Vector3(moveJoystick.position.x, 0, moveJoystick.position.y));
}
To be perfectly honest, it looks like a rather intricate script so I’d rather not dig through the script to try and understand it
lol, not to mention it would clearly be a bit more complicated.
Lol yeah from what I understand about the script (which isn’t much) it seems pretty complicated. Well I guess I’ll try and figure out how to understand it and go from there. Thanks again for all your help!
While messing around I figured it out, I set “guiBoundary.min.x” and “guiBoundary.max.x” both to 100 so that the joystick couldn’t move to either side from the center and I set “guiBoundary.min.y” to 50 which stopped the joystick from moving back from the center. Thanks again for all your help!