Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 bundy2k6 · Apr 14, 2013 at 10:19 AM · 2dplatformeritweencontrols

Original controler script with iTween

Hey guys,

I am making a 2.5D style platformer that is basically on rails. First I setup the controls and had a character jumping running and ducking. Next I used iTween to draw the path that I want the player to follow. I used one of iTween's example scripts and am wondering how to alter it so that the player is controlled by my original script rather than the built in iTween controls, they are conflicting. I want to do this because I have all animations and controls already setup on the other script. Any changes I have tried to make cut the characters ability to follow the predefined path that I set up, he just runs straight ignoring the path.

Here's the iTween code,

Any help much appreciated!

using System.Collections;

public class Controller : MonoBehaviour { public Transform[] controlPath; public Transform character; public enum Direction {Forward,Reverse};

 private float pathPosition=0;
 private RaycastHit hit;
 private float speed = .2f;
 private float rayLength = 5;
 private Direction characterDirection;
 private Vector3 floorPosition;    
 private float lookAheadAmount = .01f;
 private float ySpeed=0;
 private float gravity=.5f;
 private float jumpForce=.12f;
 private uint jumpState=0; //0=grounded 1=jumping
 
 void OnDrawGizmos(){
     iTween.DrawPath(controlPath,Color.blue);    
 }    
 
 
 void Start(){
     //plop the character pieces in the "Ignore Raycast" layer so we don't have false raycast data:    
     foreach (Transform child in character) {
         child.gameObject.layer=2;
     }
 }
 
 
 void Update(){
     DetectKeys();
     FindFloorAndRotation();
     MoveCharacter();
     
 }
 
 
 void DetectKeys(){
     //forward path movement:
     if(Input.GetKeyDown("right")){
         characterDirection=Direction.Forward;    
     }
     if(Input.GetKey("right")) {
         pathPosition += Time.deltaTime * speed;
     }
     
     //reverse path movement:
     if(Input.GetKeyDown("left")){
         characterDirection=Direction.Reverse;
     }
     if(Input.GetKey("left")) {
         //handle path loop around since we can't interpolate a path percentage that's negative(well duh):
         float temp = pathPosition - (Time.deltaTime * speed);
         if(temp<0){
             pathPosition=1;    
         }else{
             pathPosition -= (Time.deltaTime * speed);
         }
     }    
     
     //jump:
     if (Input.GetKeyDown("space") && jumpState==0) {
         ySpeed-=jumpForce;
         jumpState=1;
     }
 }
 
 
 void FindFloorAndRotation(){
     float pathPercent = pathPosition%1;
     Vector3 coordinateOnPath = iTween.PointOnPath(controlPath,pathPercent);
     Vector3 lookTarget;
     
     //calculate look data if we aren't going to be looking beyond the extents of the path:
     if(pathPercent-lookAheadAmount>=0 && pathPercent+lookAheadAmount <=1){
         
         //leading or trailing point so we can have something to look at:
         if(characterDirection==Direction.Forward){
             lookTarget = iTween.PointOnPath(controlPath,pathPercent+lookAheadAmount);
         }else{
             lookTarget = iTween.PointOnPath(controlPath,pathPercent-lookAheadAmount);
         }
         
         //look:
         character.LookAt(lookTarget);
         
         //nullify all rotations but y since we just want to look where we are going:
         float zRot = character.eulerAngles.z;
         character.eulerAngles=new Vector3(0,zRot,0);
     }

     if (Physics.Raycast(coordinateOnPath,-Vector3.up,out hit, rayLength)){
         Debug.DrawRay(coordinateOnPath, -Vector3.up * hit.distance);
         floorPosition=hit.point;
     }
 }
 
 
 void MoveCharacter(){
     //add gravity:
     ySpeed += gravity * Time.deltaTime;
     
     //apply gravity:
     character.position=new Vector3(floorPosition.x,character.position.y-ySpeed,floorPosition.z);
     
     //floor checking:
     if(character.position.y
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 robertbu · Apr 14, 2013 at 02:27 PM 0
Share

As an idea based on your question rather than fully understanding your code. Could you use an empty game object for the iTween and then make your character a child object of the empty game object? You might have to change your other script to use localPosition for movement.

avatar image bundy2k6 · Apr 14, 2013 at 02:41 PM 0
Share

Hey robertbu!

I don't fully understand, how would I make the Character a child of the empty game object? Is it in the script or something I can do in the editor? I was hoping I could basically keep the ability to move along the path but take out the built in controls, see I have the movement, animations ect working on another script but when I start the game with the controller(itween) script on, both controls conflict so my character gets all jerky and faces the wrong way.

avatar image bundy2k6 · Apr 14, 2013 at 02:57 PM 0
Share

Figured it out, I had changed the player's layer to ignore raycast but must not have saved or something, flicked that back over and it stopped the jumpiness. Also realized that the way I brought the level in is not on the same axis as the code so a couple of $$anonymous$$or tweeks and it's all fixed, both scripts are running but it's running smooth, so until I have more time to fix this... it's working!

Thanks for the response anyways robertu!

avatar image HikerJB · Jul 18, 2013 at 04:56 PM 0
Share

I'm using that sample sample script in a hiking fitness game. What I did was just move all the pathpoints to the positions I wanted and then duplicated the pathpoints because I needed more. I copied the controller script that came with the sample and made it react to my different input instructions. Hope that helps. Now I have a questions for you. How can I keep the thing from looping! $$anonymous$$y walker keeps jumping back to the beginning when he reaches the end of his trail. I want the controller to just stop, if a new path hasn't been selected.

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.

Follow this Question

Answers Answers and Comments

12 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 avatar image avatar image

Related Questions

My character moves in seemingly random directions. 1 Answer

2D Platformer (Unity 4.3) - How are the AudioClip arrays initialized? 1 Answer

Unity 2D player sticks on platform corners 2 Answers

Dynamically Created Edge Collider Woes 0 Answers

Problem with rotated colliders 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges