- Home /
Clamps won't work!
I have 2 problems. I am trying to create a endless runner type game and I decided I needed to clamp the distance the player can move up , down , left and right on the road, so I made some code for clamping the left and right sides of the road , but the player only seems to acknowledge the clamp on the left side , and I tried to clamp how much the player can go up and down the road but it doesn't work. Please help.
Here is my code for the right and left clamp.
void Update () {
//moves left and right along x axis
transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal")*movespeed);
Vector3 tmpPos = transform.position;
tmpPos.x = Mathf.Clamp(tmpPos.x, -8.0f, 9.0f);
transform.position = tmpPos; }
}
Code for Up and down clamp that won't work.
void Update () {
Vector3 tmpPos = transform.position;
tmpPos.y = Mathf.Clamp(tmpPos.y, -5.0f, 5.0f);
transform.position = tmpPos; }
}
Answer by YoungDeveloper · Aug 11, 2015 at 06:47 PM
You should merge the two scripts.
Cache the vector3 of the transform position
Check for input (left,right,up,down,..) and apply for cached vector3
clamp cached vector3
apply cached vector 3 to transform position
Nice Job. And yeah, the two scripts should be merged. No reason to have two separate scripts modifying a single objects position. That's when you start running into issues.
Thanks guys. But I'm a bit confused YoungDeveloper are you basically saying I should keep the code how it is, merge the two scripts ,but just check for input?
You should do it in order I wrote, your logic will not encounter any problems.
Your wording is confusing me a bit. So Basically what you are trying to say is I should do something like this......( I am confused , so can you explain a bit better ( in depth) , I am also confused with the merging scripts)
Vector3 tmpPos = transform.position;//cached Vector 3
if (Input.GetAxis ("Horizontal"))// gave me some errors but I am just asking if this is what you are trying to get me to do
tmpPos.x = $$anonymous$$athf.Clamp(tmpPos.x, -8.0f, 9.0f);// clamp cached Vector 3?
transform.position = tmpPos; // apply cached Vector 3 to transform position }
}
$$anonymous$$y wording is confusing? $$anonymous$$aybe you should reread what I wrote more than 1 time. I wrote a script specifically for you. It does exactly what I wrote.
using UnityEngine;
public class Basic : $$anonymous$$onoBehaviour {
private float _speedForward = 2f;
private float _speedSide = 2f;
private float _clampLeft = -1f;
private float _clampRight = 1f;
private Vector3 _cache;
private void Update (){
_cache = transform.position;
//FORWARD
_cache += Vector3.forward * _speedForward * Time.deltaTime;
//SIDES
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) _cache += Vector3.left * _speedSide * Time.deltaTime;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) _cache += Vector3.right * _speedSide * Time.deltaTime;
//CLA$$anonymous$$P SIDES
_cache.x = $$anonymous$$athf.Clamp(_cache.x, _clampLeft, _clampRight);
transform.position = _cache;
}
}
Your answer
Follow this Question
Related Questions
how to make the sprite and the background color in a tile disappear? 0 Answers
Need help in typing project 0 Answers
LineRenderer static movement 1 Answer
(Game Build) Validating files and updating to latest version 0 Answers
Timer doesn't count 0 Answers