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 /
This question was closed Apr 25, 2019 at 11:45 AM by K-Anator for the following reason:

Problem is outdated

avatar image
0
Question by K-Anator · Mar 12, 2017 at 03:20 PM · physicsrigidbodyplayer movement

R-Type style movement

Hi there, I'm working on a 2.5D game similar to the game R-Type. In that game, you have a ship which is constantly moving forward through space, though within that space you can move around freely on the vertical and horizontal axis.

So far I've gotten this working, but I don't think this is the way to go about it.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour
 {
     [SerializeField]
     float maxSpeed;
     [SerializeField]
     float minSpeed;
 
     private void Start()
     {
         // move the player forward by default
         GetComponent<Rigidbody>().velocity = (transform.forward * minSpeed);
     }
 
     void Update()
     {
         // get current velocity
         Vector3 currentSpeed = GetComponent<Rigidbody>().velocity;
         //Debug.Log("Player velocity is:" + currentSpeed.z);
        
         // get inputs
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
 
         Vector3 movement = new Vector3(0.0f, moveVertical, moveHorizontal); 
 
         // use inputs to modify velocity
         if (moveHorizontal != 0 && currentSpeed.z >= minSpeed && currentSpeed.z <= maxSpeed) //is the player pressing forward and moving within it's limit?
         {
             GetComponent<Rigidbody>().AddForce(0.0f, 0.0f , movement.z * maxSpeed);
         }
 
         if (currentSpeed.z < minSpeed) // if the current speed tries to go below minSpeed, reset it.
         {
             GetComponent<Rigidbody>().velocity = (transform.forward * minSpeed);
         }
 
         if (currentSpeed.z > maxSpeed) // if the current speed tries to go above maxSpeed, reset it.
         {
             GetComponent<Rigidbody>().velocity = (transform.forward * maxSpeed);
         }
 
         if (moveVertical != 0) // is the player moving up/down
         {
             GetComponent<Rigidbody>().transform.Translate(new Vector3(0.0f, movement.y, 0.0f));
         }
     }
 }

I just feel like I'm missing something here... I just need the player to constantly move forward on the Z axis, but allow the player to move forward and backward within set limits.

Another way I was able to tackle this was via moving the world along -Z faster than the player could move backwards. Not a good option as far as I can tell.

Comment
Add comment · Show 1
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 TreyH · Mar 12, 2017 at 03:39 PM 0
Share

The use of Rigidbody and physics-based movement in general here seems problematic.

  1. Cosmetically, constantly getting the Rigidbody component for your object every Update() isn't typically required.

  2. $$anonymous$$anually setting velocity doesn't always produce the result you'd expect (consider using .AddForce() with the appropriate Force$$anonymous$$ode ins$$anonymous$$d).

  3. On top of manually setting velocity, you are also translating during the same call.

  4. The reasons for #2 and #3 are because the physics calculations are done in concert with FixedUpdate(), so any physical interactions will be suspended until that cycle. With all of this being done in Update(), you may run into odd behavior.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

5.4 broke rigidbody.addForce? 0 Answers

Player is Speeding up in collisions 0 Answers

Camera becomes shaky on collision 0 Answers

Rigidbody cube randomly stops moving? 1 Answer

transform.forward not always forward? 2 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