Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 · Apr 13, 2017 at 11:54 AM · rotationlerpmovetowards

keep player centred in grid based movement

What I am trying to do is have the player move from tile to tile like you would in pokemon etc only this movement will eventually be an infinite runner (for now its on getKey) and the player should only be allowed to turn when they reach the centre of a tile thus keeping the player contained to a grid based movement,

This could be tedious if they miss the centre of the tile so i want to set a boolean when the player presses left or right between x and y and perform the action when the player is in the middle of the tile.

I have followed a tutorial online to make a pokemon equivalent where i have a custom plane and a cube (player) with a script attached that essentially has a start point an endpoint and Lerps between the two and I am trying to code my extras on top by setting a boolean when the user can press left or right and checking the boolean when the user reaches the end of the lerp (not the centre of the tile which maybe my mistake) but this only really works when the speed is low when i increase the speed it starts turning seemingly whenever it feels like it, can anyone help me I'm still new to unity,

Here is what i have so far

 public class player_movement : MonoBehaviour {

 public Vector3 startPoint;
 public Vector3 endPoint;
 public float speed;
 public float increment;
 public bool isMoving;
 public bool turnLeft;
 public bool turnRight;

 // Use this for initialization
 void Start () {

     startPoint = transform.position;
     endPoint = transform.position;
     
 }
 
 // Update is called once per frame
 void Update () {

     if(increment <= 1 && isMoving == true)
     {
         increment += speed / 100;
     }
     else
     {
         isMoving = false;
     }
     if(increment >= 1 && turnLeft)
     {
         increment = 0;
         isMoving = true;
         startPoint = transform.position;
         endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z);
         transform.Rotate(0, -90, 0);
         turnLeft = false;
         turnRight = false;
     }
     if (increment >= 1 && turnRight)
     {
         increment = 0;
         isMoving = true;
         startPoint = transform.position;
         endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z);
         transform.Rotate(0, 90, 0);
         turnRight = false;
         turnLeft = false;
     }

     if (isMoving)
         transform.position = Vector3.Lerp(startPoint, endPoint, increment);
    
     if(Input.GetKey("w") && isMoving == false)
     {
         increment = 0;
         isMoving = true;
         startPoint = transform.position;
         if(transform.eulerAngles.y == 0)
         {
             endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
         }
         if (transform.eulerAngles.y == 90)
         {
             endPoint = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
         }
         if (transform.eulerAngles.y == 180)
         {
             endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
         }
         if (transform.eulerAngles.y == 270)
         {
             endPoint = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
         }
          //endPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
     }
     else if (Input.GetKey("a") && increment > 0.5 && increment < 0.95)
     {
         turnLeft = true;
         turnRight = false;
     }
     else if (Input.GetKey("d") && increment > 0.5 && increment < 0.95)
     {
         turnRight = true;
         turnLeft = false;
     }

 }
 }



ok ive changed my script to use moveTowards but still no joy heres the new script

 using UnityEngine;
 using System.Collections;
 
 public class grid_movement : MonoBehaviour
 {
 public float speed = 2.0f;
 public bool turnRight;
 public bool turnLeft;
 private Vector3 pos;
 private Transform tr;

 void Start()
 {
     //Here we set the Values from the current position
     pos = transform.position;
     tr = transform;
 }

 void Update()
 {
     Movement();

 }

 private void Movement()
 {
     //If we press any Key we will add a direction to the position ...
     //using Vector3.'anydirection' will add 1 to that direction


     //But we Check if we are at the new Position, before we can add some more
     //it will prevent to move before you are at your next 'tile'
     if (Input.GetKey(KeyCode.D))
     {
         //pos += Vector3.right;
         turnRight = true;
         turnLeft = false;
         //tr.Rotate(0, 90, 0);
     }
     else if (Input.GetKey(KeyCode.A))
     {
         //pos += Vector3.left;
         turnLeft = true;
         turnRight = false;
         //tr.Rotate(0, 90, 0);
     }
     else if (Input.GetKey(KeyCode.W))
     {
         //this works but i want it to move automatically
         //pos += Vector3.forward;
     }
     else if (Input.GetKey(KeyCode.S))
     {
         //pos += Vector3.back;
     }

     //this moves automatically but wont allow me to turn

     if (turnLeft && tr.position == pos)
     {
         tr.Rotate(0, 90, 0);
         turnRight = false;
         turnLeft = false;
     }
     else if (turnRight && tr.position == pos)
     {
         tr.Rotate(0, -90, 0);
         turnRight = false;
         turnLeft = false;
     }
     if(!turnRight && !turnLeft)
     {
         pos += Vector3.forward;
     }

     transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
     //Here you will move Towards the new position ...

 }
 }

Comment
Add comment
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

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

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

Related Questions

Keep "ball" character's face rotating to be up? 0 Answers

Why isn't my object lerping ? C# 2 Answers

fromtorotation inacurate Help fixing? 0 Answers

How can I make a ball roll when using Vector3.MoveTowards? 0 Answers

How can I make smooth steps around an object? 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