Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by martipello · Dec 13, 2015 at 07:14 PM · rotationvector3velocitylerp

Help! Player script one step at a time add rotate and jump

hi ive set up a script to mimic pokemons tile by tile movement system this worked fine but i wanted something slightly different imagine temple run on an open plane, dependant on an invisible grid for movement. so the player should be constantly moving forward, rotation just rotates on the spot and the player carries on going straight

right now im depending on a start point and ending point and lerping from one to the other which isnt as smooth as it maybe could be but im still a noob at unity and scripting. my main issue is the way ive used my vector 3 its constantly adding to the x axis so when i rotate 90' it looks like im moving to the side because the script doesnt know ive rotated

anything in multi line comments doesnt work correctly but is what ive tried using, (it rotates the player but i dont know how to update the rest to say if (rotation == 90) { do this }

 public int rotateSpeed;
 public float speed;
 public float increment;
 public float distToGround;
 public float jumpSpeed;
 public Vector3 startPoint;
 public Vector3 endPoint;
 public bool isMoving;
 public bool isGrounded;
 public Rigidbody rb;
 public CapsuleCollider sonicDroid;

 void Start () {
     startPoint = transform.position;
     endPoint = transform.position;
     rotateSpeed = 90;
     jumpSpeed = 6.0f;
     rb = sonicDroid.GetComponent<Rigidbody>();
     rb.AddForce(1, 1, 1);
     distToGround = sonicDroid.bounds.extents.y;

 }
 void Update () {
 if(increment <=1 && isMoving == true)
     {
         increment += speed/10;
     }
     else
     {
         isMoving = false;
         isGrounded = true;
     }
     if (isMoving)
         transform.position = Vector3.Lerp(startPoint, endPoint, increment);
     if (isMoving == false)
     {
         increment = 0;
         isMoving = true;
         startPoint = transform.position;
         endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
     }

    //turn left
     /**
     else if(Input.GetKey("a")&& isMoving == true){
      increment = 0;
      isMoving = true;
      startPoint = transform.position;
      endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z);
      transform.Rotate(Vector3(0,90,0));
      }
      **/
     //turn right
     /**
      else if(Input.GetKey("d")&& isMoving == true){
      increment = 0;
      isMoving = true;
      startPoint = transform.position;
      endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z);
      transform.Rotate(Vector3(0,90,0));
   }
  **/

    else if (Input.GetKeyDown("space")&& isGrounded == true)
     {
         rb.velocity += Vector3.up * jumpSpeed;
         isMoving = false;
         isGrounded = false;
     }
 }
  }

it also wont jump? which is strange as i think my code i okay? and and all suggestions welcome

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 adelphiaUK · Dec 13, 2015 at 10:55 PM 0
Share

@martipello I'm just about to switch off for the night but if you haven't had any answers when I get back on tomorrow I'll have a look. This is more of a re$$anonymous$$der for me to do that than anything else. Please let me know if you get it sorted though so I won't waste my time.

avatar image martipello adelphiaUK · Dec 13, 2015 at 11:03 PM 0
Share

@adelphiaU$$anonymous$$ I've also switched off for the night, its Sunday! But no joy thus far, please if your up for it have a crack and let me know, thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by adelphiaUK · Dec 14, 2015 at 12:09 PM

OK @martipello, I've had a play but I'm not sure if this is 100% what you are after, but give it a go anyway. You will notice I have used different methods altogether and have also changed some variables from public to local. I created a new package from scratch so if you want to see what this does by itself, let me know and I'll simply add the package in.

 using UnityEngine;
 using System.Collections;
 
 public class Move : MonoBehaviour {
 
     public float rotateSpeed = 0.1f;
     public float speed = 0.1f;
     public float jumpSpeed = 6.0f;
     public float maxDistance = 1.0f;
 
     float distToGround;
     float journeyDistance;
     float startTime;
     bool isGrounded = false;
     bool doMovement = false;
     Vector3 startPoint;
     Vector3 endPoint;
     Rigidbody rb;
     Transform sonicDroid;
 
     void Awake() {
         sonicDroid = transform;
         rb = GetComponent<Rigidbody>();
     }
 
     void Start() {
         startPoint = sonicDroid.position;
         endPoint = startPoint;
         startTime = Time.time;
         rb.AddForce(1, 1, 1);
         journeyDistance = Vector3.Distance(startPoint, endPoint);
     }
 
     void Update() {
         if(isGrounded) {
             if(Input.GetKeyDown("space")) {
                 doMovement = false;
                 isGrounded = false;
                 rb.velocity = Vector3.up * jumpSpeed;
             } else if(doMovement) {
                 if(Input.GetKey("a")) {
                     sonicDroid.Rotate(new Vector3(0, 90 * rotateSpeed, 0));
                 } else if(Input.GetKey("d")) {
                     sonicDroid.Rotate(new Vector3(0, -90 * rotateSpeed, 0));
                 }
                 float distanceMoved = (Time.time - startTime) * speed;
                 float journeyFraction = distanceMoved / journeyDistance;
                 sonicDroid.position = Vector3.Lerp(startPoint, endPoint, journeyFraction);
                 float distance = Vector3.Distance(sonicDroid.position, endPoint);
                 if(distance < maxDistance) {
                     Debug.Log("distance:" + distance);
                     startPoint = sonicDroid.position;
                     endPoint += (sonicDroid.forward * speed);
                 } else {
                     Debug.Log("distance:" + distance);
                 }
             }
         }
     }
 
     void OnCollisionEnter(Collision collision) {
         if(!isGrounded && collision.collider.name.ToLower() == "floor") {
             isGrounded = true;
             doMovement = isGrounded;
             if(isGrounded) {
                 startPoint = sonicDroid.position;
                 endPoint = startPoint + (sonicDroid.forward * speed);
             }
         }
     }
 }
 
Comment
Add comment · Show 3 · Share
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 martipello · Dec 14, 2015 at 02:40 PM 0
Share

okay this didn't work at all (probably my fault) did literally nothing and i think its due to a custom plane i am using (low polys), so i set up a new scene, a new capsule and a new plane to put the capsule on and added a rigidbody to the capsule, but the capsule just falls over, any ideas what im missing? Thank you very much for your time and effort though, hope we can get this working

avatar image adelphiaUK martipello · Dec 14, 2015 at 02:47 PM 0
Share

You need to enable rotation constraints on the Rigidbody for X and Z. I used a custom plane too in my test project. The plane must be called "floor" in order for the collision detection to work or simply edit the code to check for whatever name you call the plane. You could alternatively, use a tag (ins$$anonymous$$d of name) for collision checking; I just prefer to use name as it's specific to a single object then, as long as your game object names are unique of course..

Are you on Google+ at all? I can always hook up with you live using hangouts if you are.

avatar image martipello adelphiaUK · Dec 14, 2015 at 03:25 PM 0
Share

yes i am, and that would be awesome this is my profile https://plus.google.com/u/0/+martinseal/posts also i just tried again in my test scene but applied a collider to the plane and now everything runs almost perfectly hope to hear from you soon

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

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

Rotate LOCAL y axis to match Joystick input 0 Answers

Help with raycast for for wall detection 1 Answer

Rotating, moving and keep on going with constant velocity in direction of click 1 Answer

Z axis not tracking 0 Answers

Quaternion lerp slowing down movement? 1 Answer


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