odd gravity problem.

Hi, total amateur here. Last time I dealt with coding was…jeez, almost 10 years ago
I’m building a sort of top-down game that has jumping and platforming, and gravity is refusing to work (kind of a necessity).

I started with a player movement script from the Tornado Twins’ Wormageddon tutorial:

var speed = 3.0;
var rotateSpeed = 3.0;

function Update ()
{
	var controller : CharacterController = GetComponent(CharacterController);
	
	//rotate around y axis
	transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

	//move forward, backward
	var forward = transform.TransformDirection(Vector3.forward);
	var curSpeed = speed * Input.GetAxis ("Vertical");
	controller.SimpleMove(forward * curSpeed);	
}

@script RequireComponent(CharacterController)

The gravity works fine with this script, but the motion isn’t what I’m looking for (run left/right, not turn). So, I looked around for a bit and found a fairly simple one that did what I wanted, but the gravity didn’t work. I borrowed the CharacterController elements from the Tornado Twins’ script in an attempt to trigger the missing gravity, but to no avail:

function Update () {
	var controller : CharacterController = GetComponent(CharacterController);

	if (Input.GetKey ("up")) {
	transform.Translate(0,0,.1);
	}
	if (Input.GetKey ("down")) {
	transform.Translate(0,0,-.1);
	}
	if (Input.GetKey ("right")) {
	transform.Translate(.1,0,0);
	}
	if (Input.GetKey ("left")) {
	transform.Translate(-.1,0,0);
	}
}

@script RequireComponent(CharacterController)

The CharacterController aside, what am I missing from the Twins’ script, and how might I implement it?

In the interest of getting a confirmation out of an educated guess, is it curSpeed? Should I add a speed var and have a curSpeed equal it? Should I perhaps just include curSpeed = 0.3 or something?

Thanks in advance.

This part is wrong, because you are using Translate instead of Move.

function Start()
{
    // Make it a habit to call GetComponent only at the start and not every frame
    var controller : CharacterController = GetComponent(CharacterController);
}
function Update () 
{
    if (Input.GetKey ("up")) 
    {
        controller.Move(0,0,.1);
    }
    if (Input.GetKey ("down")) 
    {
        controller.Move(0,0,-.1);
    }
    if (Input.GetKey ("right")) 
    {
        controller.Move(.1,0,0);
    }
    if (Input.GetKey ("left")) 
    {
        controller.Move(-.1,0,0);
    }
}

Try this to see if it works. :smile:

maybe the unity platform tutorial can give you a starting point.

@zine92

Assets/scripts/NewBehaviourScript.js(22,9): BCE0005: Unknown identifier: ‘controller’.

weird, huh?
moving it back to the update fixes the finding issue but raises new issues with the actual commands

@exiguous
yeah, i’ll give it a shot

Shit. Wrong code.

var controller : CharacterController;

function Start()
{
    controller = GetComponent(CharacterController);
}

Oops. A stupid mistake i made.

@zine92

well, it’s a step forward, but…

Assets/scripts/NewBehaviourScript.js(23,24): BCE0017: The best overload for the method ‘UnityEngine.CharacterController.Move(UnityEngine.Vector3)’ is not compatible with the argument list ‘(float, int, int)’.

ouch. well, thanks for trying