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 /
avatar image
0
Question by Nixeor · Sep 21, 2017 at 03:10 AM · unity 5playertranslateorigin

Trying to make my Game Object stop at Origin

I'm new to Unity, and I'm writing a script to move a player game object between one of three locations. The Center of the three locations is at origin (0,0,0), and the other two locations are found at (-2,0,0) and (2,0,0). My player begins in the center and I'm able to translate my player game object from left to right without moving past the 2 and -2 positions, thanks to clamping the player movement. However, I'm unable to make the player game object stop precisely at origin.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; }

public class PlayerController : MonoBehaviour {

 private Rigidbody rb;
 public float speed;
 public Boundary boundary;

 public KeyCode moveL;
 public KeyCode moveR;

 public float horizVel = 0;
 public int laneNum = 2;
 public string controlLocked = "n";


 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }


 void Update()
 {
     GetComponent<Rigidbody>().velocity = new Vector3 (horizVel, 0, 0);

     if ((Input.GetKeyDown (moveL)) && (laneNum > 1) && (controlLocked == "n"))
     {
         horizVel = -10;
         laneNum -= 1;
         controlLocked = "y";
     }

     if ((Input.GetKeyDown (moveR)) && (laneNum < 3) && (controlLocked == "n"))
     {
         horizVel = 10;
         laneNum += 1;
         controlLocked = "y";
     }


 }

 void FixedUpdate()
 {
     rb.position = new Vector3
     (Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax), 0.0f, 0.0f);
 }


}

Any help is appreciated. Thanks in advance

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 MT369MT · Sep 21, 2017 at 05:21 AM 0
Share

Hi Try with: transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Target.position, 1 * Time.DeltaTime);

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Sep 21, 2017 at 01:36 PM

First of all, you're mixing Update with FixedUpdate. Since you're working with the rigidbody's velocity, you should be using FixedUpdate exclusively.
Secondly, you're changing it before reading input, which is kind of the wrong order.
And finally, instead of clamping the final position, you should clamp the vector by which the velocity is changed to reach the target you're trying to reach. This way, you'll be able to reach every intermediate position.

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

Answer by Nixeor · Sep 23, 2017 at 01:10 AM

Ok.

  1. I stopped mixing the update with FixedUpdate.

  2. I also added an IEnumerator (from a Youtube tutorial) to help stop the player character.

  3. This is to @MT369MT. I've seen the Vector3.MoveTowards code before, but I'm not sure how to implement it. (I'm a newbie)

    Here is my updated code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; }

public class PlayerController : MonoBehaviour {

 private Rigidbody rb;
 public float tilt;
 public float speed;
 public Boundary boundary;

 public KeyCode moveL;
 public KeyCode moveR;

 public float horizVel = 0;
 public int laneNum = 2;
 public string controlLocked = "n";

 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }


 void FixedUpdate()
 {
     GetComponent<Rigidbody>().velocity = new Vector3(horizVel, 0, 0);

     if ((Input.GetKeyDown(moveL)) && (laneNum > 1) && (controlLocked == "n"))
     {
         horizVel = -10;
         StartCoroutine(stopSlide());
         laneNum -= 1;
         controlLocked = "y";
     }

     if ((Input.GetKeyDown(moveR)) && (laneNum < 3) && (controlLocked == "n"))
     {
         horizVel = 10;
         StartCoroutine(stopSlide());
         laneNum += 1;
         controlLocked = "y";
     }

     {
         rb.position = new Vector3
         (Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax), 0.0f, 0.0f);

         rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
     }


 }


 IEnumerator stopSlide()
 {
     yield return new WaitForSeconds(.193f);
     horizVel = 0;
     controlLocked = "n";
 }

} alt text


untitled-1.jpg (27.9 kB)
Comment
Add comment · 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

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

149 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 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

car seems to slightly lag or 'hickup' when moving 0 Answers

I need help with prefabs 1 Answer

Player Skins shop Menu 0 Answers

Moving Rigidbody with Translate causes vibrating player when walking into objects 2 Answers

Currency not getting added between levels 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