x


[Closed] Character Gravity Problem: Character Flies Upward!

Edit: A new script has been made to replace the old one. Only the new script is shown. The enemy no longer flies upward, but is currently not dropping downward.

I am making a game for a project where the main character has to defeat enemies to continue. The problem is that the enemies drop downward, but then suddenly fly upward! I have tried to keep the character down, but attempts have been unsuccessful. Before this point, the character would just float in space... Here is the code I currently have on the enemy:

The Character Controller

A Capsule Collider (for other events)

This script (in Javascript):

#pragma strict

// Set variables related to movement and individual states.

var idleTime : float = 3.1;
var marManMoveSpeed : float;
var marManRotateSpeed : float;
var gravity : float;
var attackTarget : Transform;
var chaseDistance : float;
var attackDistance : float;
private var moveDirection : Vector3 = Vector3.zero;
private var wait : boolean = false;

// Set variables for the states.
private var idle : int = 0;
private var walk : int = 1;
private var attack : int = 2;
private var status : int = idle;
var controller : CharacterController;
controller = GetComponent(CharacterController);

function Update(){
	var controller : CharacterController = GetComponent(CharacterController);
	
	var aggro : float = Vector3.Distance(attackTarget.position,transform.position);

	print(aggro);

	//Define what causes the states.
	if (aggro < chaseDistance && aggro > attackDistance){
		status = walk;
	}
	else if(aggro < attackDistance){
		status = attack;
	}
	else if(aggro > chaseDistance){
		status = idle;
	}	

	//Always apply gravity.	
	moveDirection[2] -= gravity * Time.deltaTime;

	//State what to do in each of the states.
	if (controller.isGrounded){
		switch(status){
			case idle:
				//Currently does nothing, but will hopefully have animation here.
				break;
			case walk:
				//Insert animation later.
				//Rotation handled later in script.				

				//Set the direction for movement.
				moveDirection = attackTarget.position - transform.position;
				break;

			case attack:
				//Insert "attack" animation later. Required for end use.
				//Rotation handled later in script.
				break;
			default:
				return;				

		}
	}
	Move();
}

function Move(){
	if(!wait){
		//Handle all movement in here.
		transform.LookAt(attackTarget);
		controller.Move(moveDirection * marManMoveSpeed * Time.deltaTime);
	}
	else{
	//Wait for assigned time and do nothing!
	Debug.LogWarning("Current status - waiting for "+idleTime);
	yield WaitForSeconds(idleTime);
	return;
	}
}

@script RequireComponent(CharacterController)

The custom script has the following settings in the inspector:

 Idle Time = 3.1
 Mar Man Move Speed = 5.291004
 Mar Man Rotate Speed = 3.399924
 Gravity = 50
 Attack Target = MainM (Transform)
 Chase Distance = 0.1
 Attack Distance = 0.01
 Controller = The Enemy (Character Controller)
more ▼

asked Mar 23 '12 at 01:37 AM

You! gravatar image

You!
447 8 14 16

Does this happen even when you do not try moving your player at all?

Mar 23 '12 at 01:49 AM hijinxbassist

It happens at different times, but always happens at some time. If I do not move the player, the enemy might suddenly spring upward, but sometimes springs upward after ten seconds. When I do move the player, the enemy sometimes does spring upward immediately, while other times stays down until the player has collided with the enemy.

Mar 23 '12 at 01:53 AM You!

like the answer below, you should apply gravity all the time. Also have one set amount for gravity instead of changing it using the delta time thing. Im not sure what you are trying to do, so this may not work. Please explain if this non gravity is intended for gameplay

Mar 23 '12 at 01:58 AM hijinxbassist

I do want gravity...but it hasn't been working for me. I will try what was mentioned.

Mar 23 '12 at 02:00 AM You!

He removed it because the comments were going in circles.

The point is, a machine is useless. You have to tell it Exactly what to do. If you cannot understand the steps you are giving, the machine won't.

I am a noob, 3 months in Unity with a very very basic idea on coding. But I have learned from alot of reading, test projects, writing and deleting code.

You seem to have knowledge (I havn't even tried to use an animation or modelling program) , but don't seem to be grasping what you are telling it to do. I don't want to write your script as I am a noob, and have things going myself. But Don't Give Up.

Please try the method in my answer. (start with your enemy gameObject. write one script) . I'll break it down some more and comment in a moment.

Mar 23 '12 at 05:26 AM alucardj
(comments are locked)
10|3000 characters needed characters left

The question has been closed May 25 '12 at 11:09 PM by You! for the following reason:

The question is answered, right answer was accepted


1 answer: sort voted first

It is important to note where you have :

//Always apply gravity. 
moveDirection[2] -= gravity * Time.deltaTime;

[2] is reading the Z , you want moveDirection[1] for moveDirection.y .

myArr = Vector3 (7, 8, 9) => myArr[0]=7; myArr[1]=8; myArr[2]=9;

more ▼

answered Mar 24 '12 at 04:51 AM

alucardj gravatar image

alucardj
14k 40 57 88

from what I can decipher from this long wall of text is that : gravity should be applied all the time, not just when => if (!(controller.isGrounded))

Mar 23 '12 at 01:54 AM alucardj

I did what you suggested, and now the enemy has stopped rising...but the same thing happened, only faster. I tried removed the Character Motor script, but the enemy only floated when I did this...

Mar 23 '12 at 02:05 AM You!

Im going to move to a new answer..this is getting long and no longer refers to this answer

Mar 23 '12 at 02:16 AM hijinxbassist

yes, this is becoming very messy. The moveDirection is the vector used by the Unity Script Reference example. As @hijinxbassist said, your problem with gravity is solved , so this question is over.

You now have a New problem relating to movement.

I suggest you start again , start with the template on http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html

Then in your switch-case, instead of translating or rotating with selfplace.position ect , use the character controller command (as in the example link) to move your character after all calculations have been made

// Move the controller
controller.Move(moveDirection * Time.deltaTime);

Full read-up : http://unity3d.com/support/documentation/Components/class-CharacterController.html

Mar 23 '12 at 02:59 AM alucardj

@alucardj Thanks for your support! +=thumb buddy

Mar 23 '12 at 03:06 AM hijinxbassist
(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3570
x706
x699
x481
x298

asked: Mar 23 '12 at 01:37 AM

Seen: 1194 times

Last Updated: May 25 '12 at 11:09 PM