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 Gumbercules · Sep 22, 2014 at 09:27 AM · transformlerp

Lerp to a position while rotating around

Hi there,

I am making a racing-type game with tracks that have curves. I currently have the player lerping between each "lane" whenever they press down on the arrow keys. This works fine for translating down the z-axis, but not for any other direction. My question is, how do I lerp the player (using local positions) when the player's transform.forward is no longer on the world z-axis?

alt text

This represents the easy and working code that I have. Only working if the player is traveling down the z-axis.

alt text

And this represents the turns in my game. I can't figure out how to keep it lerping to a position that is a certain distance away, while keeping it updated through the turn.

Here is my code: using UnityEngine; using System.Collections;

 public class Player : MonoBehaviour
 {
     private Camera _mainCam;
     public bool[] _lanes = new bool[7]
     {
         false,false,false,false,false,false,false
     };
 
     public int _currentLane = 3;
     public Vector3 _newLane;
     public bool _changingLane = false;
     public bool _moveLeft;
 
     private bool _inGame = false;
 
     //public int playerHealth;
     public const float BASE_SPEED = 20f;
     public float _currentSpeed;
     public float _strafeSpeed = 20f;
 
     public bool inGame
     {
         get
         {
             return _inGame;
         }
         set
         {
             _inGame = value;
         }
     }
 
     // Use this for initialization
     void Start ()
     {
         _mainCam = Camera.main;
 
         // Only set the middle lane true before game starts
         doSetLane(7, true);
 
         _currentSpeed = 0;
     }
     
     // Update is called once per frame
     void Update()
     {
         this.transform.position += this.transform.forward * Time.deltaTime * 10f;
         if(!_changingLane)
         {
             _listenForMovement();
         }
         else
         {
             _doChangeLane();
         }
     }
 
     private void _listenForMovement()
     {
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             if(_currentLane == 0)
             {
                 Debug.Log("Can't move left.");
             }
             else
             {
                 --_currentLane;
                 _moveLeft = true;
                 _changingLane = true;
                 _newLane = this.transform.TransformPoint (new Vector3(-1.5f, 0, 0));
                 Debug.Log("Moved left");
             }
         }
         
         if (Input.GetKey(KeyCode.RightArrow))
         {
             if(_currentLane == _lanes.Length - 1)
             {
                 Debug.Log("Can't move right.");
             }
             else
             {
                 ++_currentLane;
                 _moveLeft = false;
                 _changingLane = true;
                 _newLane = this.transform.TransformPoint (new Vector3(1.5f, 0, 0));
                 Debug.Log("Moved right");
             }
         }
     }
 
     /**
      * public void doSetLane ( int, bool )
      * 
      * @description
      * Sets the lane(s) provided to the set condition.
      * This tells the game if the player can travel the lane or fall through.
      * 
      * @param index {int} Lane to be changed
      * @param condition {bool} Turn the lane on or off
      */
     public void doSetLane(int index, bool condition)
     {
         if (index >= _lanes.Length)
         {
             for (int i = 0; i < _lanes.Length; ++i)
                 _lanes [i] = condition;
         }
         else if (index >= 0 && index < _lanes.Length)
         {
             _lanes [index] = condition;
         }
         else
         {
             Debug.Log("Player.cs@doSetLane(int,bool): Lane index out of range! index == " + index);
         }
     }
     
     /**
      * public void _doMovePlayer ()
      * 
      * @description
      * Moves the player left or right.
      */
     private void _doChangeLane()
     {
         //Vector3 tmp = this.transform.position;
         _newLane.z = this.transform.position.z;
         if(_moveLeft)
         {
             this.transform.position = Vector3.Lerp(this.transform.position, _newLane, _strafeSpeed * Time.deltaTime);
         }
         else
         {
             this.transform.position = Vector3.Lerp(this.transform.position, _newLane, _strafeSpeed * Time.deltaTime);
         }
     
         /*Debug.Log ("x: " + (this.transform.position.x + (this.transform.position.x - tmp.x)));
         Debug.Log ("z: " + (this.transform.position.z + (this.transform.position.z - tmp.z)));
         _newLane.x = this.transform.position.x + (this.transform.position.x - tmp.x);
         _newLane.z = this.transform.position.z + (this.transform.position.z - tmp.z);*/
 
         if(Vector3.Distance(this.transform.position, _newLane) <= 0.01f)
         {
             this.transform.position = _newLane;
             _changingLane = false;
         }
     }
 }
 
1.png (9.7 kB)
2.png (40.4 kB)
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

2 People are following this question.

avatar image avatar image

Related Questions

How do I smooth out my crouch camera movement 0 Answers

Mathf.Lerp Question 2 Answers

Smooth swapping my game object 0 Answers

Need to check transform.rotation 1 Answer

Learning to move 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