- Home /
Question by
TRiToNDREyJA · Jan 27, 2013 at 10:41 PM ·
terrainnormalsslopealign
how can I align player with slope normals(no rigidbody)
i really need to find out how to do this, ive read about raycasts and looked through answers, but none of them work. my character either rotated randomly or teleported. can someone make me a script?
Here is my char controller:
public var walkSpeed : float = 1.0; private var gravity = 60; private var moveDirection : Vector3 = Vector3.zero; private var charController : CharacterController; //private var power : float = 4.0;
public var JumpSpeed : float;
public var motor : CharacterMotor;
public var acc : float;
public var maxspeed : float;
var underwater : boolean = false;
public var motor2 : CharacterMotorJumping;
function Start() { charController = GetComponent(CharacterController); animation.wrapMode = animation.wrapMode.Loop;
targetNormal = transform.up;
}
public var pad : float = 0.0f;
var jumpingFromPad : boolean = false;
var canJumpFromPad : boolean = true;
public var streak : GameObject; // player direction in X-Z space;
public var energy : float;
public var isGrounded : boolean = false;
//Make raycast direction down
public var homing : homingattack;
private var vertVel: float = 0; // vertical velocity
function Awake () {
motor = GetComponent(CharacterMotor);
}
function Update () {
if(Input.GetKey("e") ) {
animation.CrossFade("Jump");
}
if(!isGrounded)
{
homing.canhome = true;
}
if (jumpingFromPad)
{
animation.CrossFade("Up");
}
if (canJumpFromPad && jumpingFromPad)
{
pad += Time.deltaTime;
vertVel = 100;
}
if (pad >= 1.5f && jumpingFromPad)
{
pad = 0;
jumpingFromPad = false;
homing.canhome = true;
}
var distToGround: float;
isGrounded = Physics.Raycast(transform.position, -Vector3.up, distToGround + 1.5);
if(Input.GetKey("left shift")&& isGrounded) {
energy = 3;
}
if(Input.GetKey("e") ) {
animation.CrossFade("Jump");
animation["Jump"].speed = 20;
}
if(Input.GetAxis("Vertical") > 0.1 && isGrounded) {
if (charController.isGrounded)
{
if(Input.GetKey("left shift")) {
animation["Run"].speed = 90;
}
else {
animation.CrossFade("Run");
animation["Run"].speed = 1;
}
}
}
else
{
if (charController.isGrounded == false)
{
if(Input.GetAxis("Vertical"))
{
animation.CrossFade("Fall2");
}
if(Input.GetKey("e"))
{
}
else
{
animation.CrossFade("Fall2");
}
}
else {
if (walkSpeed >0)
{
walkSpeed -= (walkSpeed/3);
}
if (walkSpeed <1)
{
animation.CrossFade("Idle");
}
}
}
// Create an animation cycle for when the character is turning on the spot
if(Input.GetAxis("Horizontal") && !Input.GetAxis("Vertical"))
{
//animation.CrossFade("walk");
}
transform.eulerAngles.y += (Input.GetAxis("Horizontal") * 5);
// Calculate the movement direction (forward motion)
// read the controls outside the if:
if (isGrounded) { // if it's grounded...
if (Input.GetKey ("e")) { // and Jump is pressed...
vertVel = 65; // vertical velocity = jumpSpeed
homing.canhome = true;
isGrounded = false;
}
}
vertVel -= gravity * Time.deltaTime; // apply gravity to the vertical velocity
moveDirection.y = vertVel; // combine move direction with vertical velocity
moveDirection = Vector3(0, vertVel, walkSpeed);
moveDirection = transform.TransformDirection(moveDirection);
charController.Move(moveDirection * Time.deltaTime); // and Move
}
//this is my movement control script to switch the animations..
function FixedUpdate () { if(Input.GetAxis("Vertical") > .1) {
streak.particleEmitter.minEnergy = energy;
streak.particleEmitter.maxEnergy = energy;
if(Input.GetKey("left shift") && isGrounded )
{
animation.CrossFade("Run");
animation["Run"].speed = 20;
walkSpeed = 200;
energy = 3;
}
else
{
animation["Run"].speed = 1;
animation.CrossFade("Run");
if (walkSpeed <= maxspeed && !underwater)
{
walkSpeed += (acc);
}
else if (underwater && walkSpeed <= (maxspeed/12))
{
walkSpeed += (acc/5);
}
else
{
if (underwater)
{
walkSpeed = (maxspeed/12);
}
else
{
walkSpeed = maxspeed;
}
energy = 0;
}
if(Input.GetKey("e") ) {
animation.CrossFade("Jump");
}
}
if(Input.GetAxis("Vertical") < -.1 && isGrounded )
{
animation["Run"].speed = -1;
animation.CrossFade("Run");
walkSpeed = 5;
}
if(Input.GetKey("e") ) {
animation.CrossFade("Jump");
}
// Create an animation cycle for when the character is turning on the spot
}
}
function OnTriggerEnter(other : Collider)
{
if (other.tag == "jumpPad")
{
print ( "jumpPad reached" );
jumpingFromPad = true;
}
if (other.tag == "water") {
underwater = true;
}
}
function OnTriggerExit ( other : Collider) {
if (other.tag == "water") {
underwater = false;
}
}
Comment
Could you make that question a bit more precise? It's a tad... unpractical to try and figure out where in over 200 lines of code a relevant situation and possible bug are.
i am making a sonic style game. my terrain has hills and rough terrain. i want the character to say for example, rotate 45 degrees if hes walking up a 45-degree slope?. i need a script for this
the code doesnt matter much. i just need to see if the jumping around is caused by the script