Can't show input direction?

General Discussion on the Unity Engine.
Post Reply
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Can't show input direction?

Post by Callan S. »

I found some code for controlling a platforming character. I'd like to know what direction was last pressed, so I can change the graphic shown on the cube used for the platforming dude. But I can't figure out how to access it to even show it!

Code: Select all

// borrowed from:

// http://answers.unity3d.com/questions/20261/how-can-i-make-double-jump-need-help.html



var WalkSpeed = 6.0;

var RunSpeed = 6.0;

var jumpSpeed = 8.0;

var gravity = 20.0;

var direction=0;



private var moveDirection = Vector3.zero;

private var grounded : boolean = false;



function FixedUpdate() {



	if (grounded) {

		// We are grounded, so recalculate movedirection directly from axes

		// moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

		moveDirection = new Vector3(Input.GetAxis("Horizontal")*-1,0,0);

		moveDirection = transform.TransformDirection(moveDirection);

		var mySpeed = WalkSpeed;



		/*

		if (Input.GetKey("left shift")) {

			mySpeed = RunSpeed;

		}

		*/

		moveDirection *= mySpeed;

		

		if (Input.GetButton ("Jump")) {

			moveDirection.y = jumpSpeed;

		}

	}



	// Apply gravity

	moveDirection.y -= gravity * Time.deltaTime;

	

	// Move the controller

	var controller : CharacterController = GetComponent(CharacterController);

	var flags = controller.Move(moveDirection * Time.deltaTime);

	grounded = (flags & CollisionFlags.CollidedBelow) != 0;



direction=Input.GetAxis("Horizontal")*-1;

GameObject.Find ("GUI Text").guiText.text="wow!: ".direction;

//.moveDirection;

}



@script RequireComponent(CharacterController)

// character controller heigh = 0.85

// radius = 0.4
More specifically it's probably a value in this line I want to show in GUI Text/be able to read

Code: Select all

moveDirection = new Vector3(Input.GetAxis("Horizontal")*-1,0,0);
But I don't seem to be able to do it?
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Can't show input direction?

Post by SpiritWebb »

A 2d platformer?

If so, here is a link for a tutorial on how to do a Mario clone, and this could help you:
http://walkerboystudio.com/html/unity_course_lab_4.html
Image

Image
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Can't show input direction?

Post by Callan S. »

Wow, thanks for the link, Spirit! I assure you I googled quite a few times and never found something like this!
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Can't show input direction?

Post by SpiritWebb »

No problem, glad I could help. If you notice, they have quite a bit of tutorials on there! :)
Image

Image
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: Can't show input direction?

Post by OldRod »

I started through the Walker Boys tutorials last year. They are pretty good, at least the ones I went through.

At the time, the Mario tutorial was locked until you completed some earlier ones. Looks like they removed that restriction. Nice! :)
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Can't show input direction?

Post by Callan S. »

Heh, and yet I've decided to start with the clicking game tutorial anyway!
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Can't show input direction?

Post by SpiritWebb »

:lol:
Image

Image
Post Reply

Return to “General Unity”