Question by
bluePhoenix99 · Jun 04, 2020 at 03:37 PM ·
scripting problemmovementobjectcollsion
How can i detect a collision with many Terrains?, since my collider is not working?
So, I am making a game but the collision does not seem to work, no collider work and etc, i use a FBX file as my character, and use this movement script:
public float speed;
public int Rot1Speed;
public int Rot2Speed;
public Transform playerBody;
public float mouseSpeed;
public GameObject cam;
private bool ShiftPressed = false;
private float speedOriginValue;
private void Start()
{
speedOriginValue = speed;
if (PlayerPrefs.HasKey("Sens"))
{
mouseSpeed = PlayerPrefs.GetFloat("Sens");
}
}
private void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSpeed * Time.deltaTime;
playerBody.Rotate(Vector3.up * mouseX);
transform.Rotate( Input.GetAxis("Mouse Y") * Rot1Speed, 0.0f, -Input.GetAxis("Horizontal"));
if(Input.GetKey(KeyCode.W))
{
transform.position -= transform.forward * Time.deltaTime * speed;
}
if(Input.GetKeyDown(KeyCode.LeftShift))
{
ShiftPressed = true;
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
speed = speedOriginValue;
ShiftPressed = false;
}
if(ShiftPressed == true)
{
speed = speedOriginValue * 2;
}
float TerrainHeightWhere = Terrain.activeTerrain.SampleHeight(transform.position);
if(TerrainHeightWhere > transform.position.y)
{
transform.position = new Vector3(transform.position.x, TerrainHeightWhere, transform.position.z);
}
}
The:
float TerrainHeightWhere = Terrain.activeTerrain.SampleHeight(transform.position);
if(TerrainHeightWhere > transform.position.y)
{
transform.position = new Vector3(transform.position.x, TerrainHeightWhere, transform.position.z);
}** part only detects certain parts of the main terrain, how should i fix this?
Comment
Your answer
Follow this Question
Related Questions
Where should the Acceleration Go?! 0 Answers
How To Stop Enemy Movement During Its Attack Animation 1 Answer
Unity 3D: Third person movement and mouse camera control 0 Answers
Object reference not set to an instance of an object [problem with Unity 5] 2 Answers
Script doesnt work properly on 1 object 0 Answers