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 · Dec 15, 2015 at 01:46 PM · positiontilecenter

smoothly going forward to the center of one tile at a time

im very close to this but still haven't succeeded, what i want is my player to move forward continuously over a plane that is divided into tiles, the player should move one tile at a time smoothly so

  endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z+1);
    

doesnt quite work (although it seems the best so far) as it steps, stops,steps,stops and i just want it to smoothly glide over each tile, but the player should only be able to rotate 90' left or right, or jump, when it is in the middle of a tile, so after some advice from someone with more experience than myself i have set up my tiles (custom plane 1x1) and set an empty gameobject on each with a OnCollisionStay listener that says if player is on a tile player can rotate or jump, this doesn't quite work as the player is not set to the tiles center, so often the player ends up on the edge of the tiles instead of staying in the center. i essentially want a big grid that the player cannot deviate from so no diagonals, what ive done so far is this

using UnityEngine; using System.Collections;

public class experimentalCharacter : 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;

     //this is where i tried changing endpoint to step once using this
    //endPoint = new Vector3(startPoint.x, startPoint.y, startPoint.z + 1);

     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
         {
             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 OnCollisionStay(Collision collision)
     {
     if (!isGrounded && collision.collider.tag.ToLower() == "floor")
     {
         isGrounded = true;
     }
         //doMovement = isGrounded;
         if (isGrounded && collision.collider.tag.ToLower() == "target")
         {
            
             startPoint = sonicDroid.position;
             endPoint = startPoint + (sonicDroid.forward * speed);

             if (Input.GetKeyDown("a"))
             {
                 sonicDroid.Rotate(new Vector3(0, 90, 0));
                 Debug.Log("you turned right");
             }else if (Input.GetKeyDown("d"))
             {
                 sonicDroid.Rotate(new Vector3(0, -90, 0));
                 Debug.Log("you turned left");
             }

         }
     
 }
 } 


any and all suggestions welcome

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

34 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

Related Questions

How to center an object to a camera, but away from it? 1 Answer

Y position of objects in Unity 1 Answer

I need to reposition the box collider of a cube and that cube is being generated at run time ? and pivot of the cube is also not correct. 0 Answers

How to get a Gameobject's origin to the center of it instead of the edge? 1 Answer

Check if position is inside a collider 5 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