• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TRiToNDREyJA · Jan 27, 2013 at 10:25 PM 0
Share

ignore the code // comments.

avatar image Golan2781 · Jan 27, 2013 at 10:48 PM 0
Share

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.

avatar image TRiToNDREyJA · Jan 27, 2013 at 11:11 PM 0
Share

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

avatar image TRiToNDREyJA · Jan 27, 2013 at 11:11 PM 0
Share

the code doesnt matter much. i just need to see if the jumping around is caused by the script

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Align plane to normal but keep Y-axis intact 1 Answer

3d ethan character can't walk up 20 Deg slope 0 Answers

How do I make my object follow the ground perfectly? 1 Answer

Make an instantiated object match the slope of the terrain 1 Answer

C# - Wipeout style Hovercraft effect 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges