- Home /
Adding force to character on CharacterMotor
Basically since im using the PlatformInputController i dont wanna use a rigidbody otherwise t'll mess up the physics so im wondering how would i go about adding force to the my character towards a certain direction. Im trying to understand how the CharacterMotor.js works but dont understand it that much.
(trying to do a wall jump btw, i want to add force to the oppsite direction to the wall im facing)
Answer by T27M · Apr 19, 2012 at 12:12 AM
This may help, but I'm not sure if it would accomplish what you needed. One of the functions of the CharacterMotor.js is SetVelocity. You can change this by passing it a variable.
The function in the CharacterMotor.js..
function SetVelocity (velocity : Vector3) {
grounded = false;
movement.velocity = velocity;
movement.frameVelocity = Vector3.zero;
SendMessage("OnExternalVelocity", SendMessageOptions.DontRequireReceiver);
}
A script I use to respawn a player which passing a varaible to the function above.
function playerRespawn ()
{
// Variable : ScriptName
var setVelocity : CharacterMotor;
// Sets a velocity, this would be not moving at all.
var myNewVelocity = Vector3(0,0,0);
// Gets the script that is attached to the same object.
setVelocity = gameObject.GetComponent("CharacterMotor");
// Passes in the new velocity
setVelocity.SetVelocity(myNewVelocity);
// Moves the player to the spawn point.
You probably wont need this part
transform.position = Vector3(256,5,256);
}
This works for what I needed, you might want to check the api for GetComponent.
thx a bunch, lot of help on this one; like i said i was trying to do a wall jump system and it works well except that it only happens only some of the time; im thinking is because i have too many if statement im trying to make it happen in one frame but im not sure
function OnTriggerStay (hit : Collider)
{
if((hit.gameObject.tag == "LeftWall") && (Input.Get$$anonymous$$ey("d")))
{
grounded = true;
if (Input.GetButtonDown ("Jump"))
{
myNewVelocity = Vector3(-18,8,0);
setVelocity.SetVelocity(myNewVelocity);
}
}
}
here's my piece of code i added in as example maybe u can spot something im not doing right?
The API says that OnTriggerStay is called "almost" every frame. I would assume that is why sometimes it works and sometimes it doesn't, not 100% sure on this one.
$$anonymous$$aybe you could try and use OnCollisionStay ins$$anonymous$$d which is called every frame.
http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionStay.html
Question.. when im doing something basic like this, why isnt it working? noob :3
function OnCollisionStay(hit : Collision)
{
if(hit.collider.tag == "ABC") //I've tried 'hit.gameObject.tag' as well
{
Debug.Log("I am colliding");
}
}
I think it's todo with the character controller not having an actual rigidbody attached.
You could try this
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.collider.CompareTag("ABC"))
{
// Changes the color of material, not needed.
hit.collider.renderer.material.color = Color.red;
Debug.Log("I am colliding");
}
}
Your answer
Follow this Question
Related Questions
rigidbody2d.addforce for is not working for the x axis 0 Answers
Kick a Ball - Add Force? 2 Answers
AddForce in multiplayer displays as curved motion 1 Answer
Getting launch angle for projectile given height, distance and speed in 3D 2 Answers
Add Force if GameObject is detected in a Polygon Collider 2D 1 Answer