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 /
This question was closed Jun 18, 2014 at 07:55 AM by Benproductions1 for the following reason:

Asking for code

avatar image
-1
Question by KennethVedder · Jun 18, 2014 at 05:25 AM · rigidbodyiosinputtouchmario

I'm trying to translate this code to work for touch devices

Hello, i'm trying to make this code below work for touch input (iOS specifically), it's a code for characters to move in a Mario Galaxy style, it works perfectly on Keyboard input, but i'm looking for a code similar or a way to make work on Touch.

I've been trying to think of a way to make it work but haven't hit any solutions.

Thank You.

 var moveSpeed: float = 6; // move speed
 var turnSpeed: float = 90; // turning speed (degrees/second)
 var lerpSpeed: float = 10; // smoothing speed
 var gravity: float = 10; // gravity acceleration
 var isGrounded: boolean;
 var deltaGround: float = 0.2; // character is grounded up to this distance
 var jumpSpeed: float = 10; // vertical jump initial speed
 var jumpRange: float = 10; // range to detect target wall
 var robCollider = gameObject.GetComponent(CapsuleCollider);
   
 private var surfaceNormal: Vector3; // current surface normal
 private var myNormal: Vector3; // character normal
 private var distGround: float; // distance from character position to ground
 private var jumping = false; // flag "I'm jumping to wall"
 private var vertSpeed: float = 0; // vertical jump current speed 
  
 function Start(){
     myNormal = transform.up; // normal starts as character up direction 
     rigidbody.freezeRotation = true; // disable physics rotation
     // distance from transform.position to ground
     distGround = collider.bounds.extents.y - robCollider.center.y;  
 }
  
 function FixedUpdate(){
     // apply constant weight force according to character normal:
     rigidbody.AddForce(-gravity*rigidbody.mass*myNormal);
 }
  
 function Update(){
     // jump code - jump to wall or simple jump
     if (jumping) return;  // abort Update while jumping to a wall
     var ray: Ray;
     var hit: RaycastHit;
     if (Input.GetButtonDown("Jump")){ // jump pressed:
         ray = Ray(transform.position, transform.forward);
         if (Physics.Raycast(ray, hit, jumpRange)){ // wall ahead?
             JumpToWall(hit.point, hit.normal); // yes: jump to the wall
         }
         else if (isGrounded){ // no: if grounded, jump up
             rigidbody.velocity += jumpSpeed * myNormal;
         }                
     }
  
     // movement code - turn left/right with Horizontal axis:
     transform.Rotate(0, Input.GetAxis("Horizontal")*turnSpeed*Time.deltaTime, 0);
     // update surface normal and isGrounded:
     ray = Ray(transform.position, -myNormal); // cast ray downwards
     if (Physics.Raycast(ray, hit)){ // use it to update myNormal and isGrounded
         isGrounded = hit.distance <= distGround + deltaGround;
         surfaceNormal = hit.normal;
     }
     else {
         isGrounded = false;
         // assume usual ground normal to avoid "falling forever"
         surfaceNormal = Vector3.up; 
     }
     myNormal = Vector3.Lerp(myNormal, surfaceNormal, lerpSpeed*Time.deltaTime);
     // find forward direction with new myNormal:
     var myForward = Vector3.Cross(transform.right, myNormal);
     // align character to the new myNormal while keeping the forward direction:
     var targetRot = Quaternion.LookRotation(myForward, myNormal);
     transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, lerpSpeed*Time.deltaTime);
     // move the character forth/back with Vertical axis:
     transform.Translate(0, 0, Input.GetAxis("Vertical")*moveSpeed*Time.deltaTime); 
    
 }
  
 function JumpToWall(point: Vector3, normal: Vector3){
     // jump to wall 
     jumping = true; // signal it's jumping to wall
     rigidbody.isKinematic = true; // disable physics while jumping
     var orgPos = transform.position;
     var orgRot = transform.rotation;
     var dstPos = point + normal * (distGround + 0.5); // will jump to 0.5 above wall
     var myForward = Vector3.Cross(transform.right, normal);
     var dstRot = Quaternion.LookRotation(myForward, normal);
     for (var t: float = 0.0; t < 1.0; ){
         t += Time.deltaTime;
         transform.position = Vector3.Lerp(orgPos, dstPos, t);
         transform.rotation = Quaternion.Slerp(orgRot, dstRot, t);
         yield; // return here next frame
     }
     myNormal = normal; // update myNormal
     rigidbody.isKinematic = false; // enable physics
     jumping = false; // jumping to wall finished
 }



Comment
Add comment · Show 2
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 HarshadK · Jun 18, 2014 at 05:32 AM 2
Share

$$anonymous$$indly tell us where you are stuck so that we can provide you the exact solution.

For start you might need to go through Touch and Input.touches

avatar image ahaykal · Jun 18, 2014 at 07:32 AM 1
Share

$$anonymous$$indly understand that the unity answers is not a place for giving free codes. It is to guide the developer. As Harshad$$anonymous$$ stated, at least try to work on your own and when you get stuck with something ask about it specifically.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Short touch delay using either Input or EventSystem classes 1 Answer

Why can't I press both my touch controls at the same time? 1 Answer

How do you map touch to one of the input axes? 1 Answer

Input.touchCount won't return 2 or more on iOS. 1 Answer

Falling object touch 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