I’d like to know different ways to limit th X axys movement character, and test to see what Works, because the ones i teste aren’t working.
A lot depends on how you want to move your character. But generally, you will have a limit maximum and minimum. Use an if statement, if, position x, > xLimitMax, set x position as xLimitMax. And vice versa for minimum.
Oh, i forgot to post code here:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class pinguim : MonoBehaviour
{
public float speed = 20.0f;
public float rotateSpeed = 3.0f;
void Start()
{
CharacterController controller = GetComponent<CharacterController>();
}
void Update ()
{
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
transform.position += transform.right * speed * Time.deltaTime;
}
Any suggestions?
I think you said something like it:
float xLimitLeft = "value"f;
float xLimitRigh = "value"f;
void Update()
{
if(transform.position.x < xLimitLeft)
{
transform.position.x = xLimitLeft;
{
if(transform.position.x > xLimitRight)
{
transform.position.x = XLimitRight;
}
}
"
or
Mathf.Clamp(transform.position.x,xLimitLeft,xLimitRight);
Ooo.
Never knew about that.
@ GustavoH,
a couple of things about your code.
- I would not worry about rotation for now. Not until you learn how to move your player and fully understand, you should not bother with juggling too many items at once.
- You are getting the CharacterController Component, but are not using it anywhere. Either do not get the component or use the component. As of now, with your code, it serves no purpose.
Well, i need rotation because it makes part of my game, albeit it is being hard i must make na effort to add it.
I actually removed CharacterController. I’ll try to add limits, i return here when i’ve results.
Ok, good luck.
Something else to sucks me hahaha
I could insert a X limit and make the character don’t go beyond these limits. But my project have some turnings, so the player won’t can turn if these limits are established. What to do now? Maybe i’ll have to improvise with a collision?
Can you describe your set up, game enviroment and what you are trying to achieve? Another option is colliders, but I find using them to set limits is tricky. Of course if you are making a game like GTA, then I suppose nessescary.
It’s basically a lane in that the player must divert the colliders, the player go forward automatically as you can see, this lane have some turnings, i want to add limits because the way is large and i want to limit the movement near the colliders.
So does the lane curve? Is it like an endless runner?
Yes
Hm. Perhaps, the road itself is a trigger, if player exits the lane trigger (onTriggerExit()) player position is set to be back in the trigger, at its edge. Essentially deny it the ability to leave the trigger.
Do you know the code to do it? Tomorrow i keep treating about this, because now it’s 23:33 PM here
Again man, to make an endless runner, if you don’t know how to code what I just told you makes me think you are being over ambitious for your skill level.
void OnTriggerExit(Collider col){
if (col.CompareTag("Player"))
col.gameObject.transform.position = new Vector3(xLimit, y, z);
}
This code have a problem. It’s because has a attributed X value in xLimit to transform, then when the collision happens the character will return to this X position, so it’ll transform straight forward and can’t turn.
I thank you for helping, but it seems i’ll have add lots of code lines to do what i want.
I’ve came to the conclusion that doesn’t help define a X limit, because X will change along the lane. So i had na interesting idea: Add collisions and make the object rotate to the other side
For example, if the character collide in the right side, make it rotate to the other side, this way it’ll not get exceed the limits.
Do you know how to do it? Sorry if i’m being ambitous, i didn’t thought that make just a lane and collisions would be hard…