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 /
This question was closed Jun 07, 2012 at 10:13 PM by NorthernEagle for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by NorthernEagle · Feb 08, 2012 at 08:24 AM · charactercontroller

CharacterController - hard code X-axis movement to limit movement?

Hi, I'm trying to use the documented script for CharacterController to control a GameObject. Can I hard code the X-axis movement to limit the range that the player can walk/jump along the same X-axis? I would like to limit the player to plus or negative 10 transform.position.x. I tried adding a Mathf.Clamp to the player.transform.position.x and it won't allow the counter in Inspector to go beyond +10 or -10 on the X-axis but the player keeps moving beyond the limit though in the scene.

Thanks in advance for any help.

Here's my source code in c#:

 // PlayerMovement.cs - main script that controls player

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{
public GameObject player;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private float posX;

 // Update is called once per frame
 void Update ()
 {
     // player movement code here
     CharacterController controller = GetComponent<CharacterController>();
     posX = player.transform.position.x;
     if (controller.isGrounded)
     {
         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         Debug.Log(posX);
         
         // check for jump here
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;
     }
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }

}

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by tingham · Feb 08, 2012 at 01:32 PM

You need to zero out the application of "velocity" to moveDirection before it is applied to the transform in controller.Move

Move is a relative positioning method, not absolute which is why you're seeing drift beyond your bounds with clamp. You won't want to allow any movement on the X in this chunk of code if your aim is to bound the player.

You can always calculate the necessary offsets inside of this block before the force is applied however this may be glitchy visually.

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 NorthernEagle · Feb 09, 2012 at 03:58 AM

Hi Tingham, thanks for the quick answer. I took your idea and ran with it to lower the speed value to 0.0F if the player position.x value is over 10.0F or smaller than -10.0F. The code below works perfect now as it stops the player dead. There's a new bug now that all velocity stops and the player is frozen. I really appreciate the help and hopefully it will help others here. I owe you a beer!

public class PlayerMovement : MonoBehaviour
{
public GameObject player;
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float posX;
public float zeroedOutSpeed = 0.0F;
private Vector3 moveDirection = Vector3.zero;

 // Update is called once per frame
 void Update ()
 {
     // player movement code here
     CharacterController controller = GetComponent<CharacterController>();
     posX = player.transform.position.x;
     if (controller.isGrounded)
     {
         // speed value code here
         if (posX > 10.0F || posX < -10.0F)
         {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= zeroedOutSpeed;
         }
         
         else
         {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
         }
         
         // check for jump here
         if (Input.GetButton("Jump"))
             moveDirection.y = jumpSpeed;
     }
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }

}

Comment
Add comment · Show 1 · 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 tingham · Feb 09, 2012 at 04:21 AM 1
Share

Very nice indeed.

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

making "one-way" platform 1 Answer

Stopping the Character Controller 0 Answers

Detecting collisions with OnControllerColliderHit when not moving 0 Answers

Conflict between CharacterController and NavMeshAgent 1 Answer

Lerp can someone explain please 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