- Home /
I walk through terrain/hills.
I'm fairly new to unity and i am having trouble with my player. When i move i move through the hills of my terrain. I have colliders and i feel its something wrong with the script. Note: I'm also new to C#
using UnityEngine;
public class Movement : MonoBehaviour
{
void Update()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
}
}
Are you sure all the colliders are enabled (of your player and terrain)?
Answer by haruna9x · Sep 01, 2017 at 04:11 PM
You need to add the Rigibody component to your character
You need to add a Collider component to your character
Inside code, do not use Translate to move character, replaced by velocity or force, or MovePosition . If you do not use Rigibody, you should use Unity's CharacterController reference controller.
I'm not new to Unity or C#, but I am new to working in 3D.
I've also written a script using translate because I'm having TONS of problems with both rigid bodies and the character controller. Generally, simply applying either of those components ends up hurtling my character into space for reasons I can't even begin to comprehend. Even the script I copied directly from the character controller manual page didn't work - and I need more functionality than it offers.
Could you point me in the direction of a script that will change my character's position relative to the camera and also turn him to face the direction he is moving in? It doesn't seem like something so simple should be so hard.
Also, I've already got all of my camera functionality built and it works fine.
DOCU$$anonymous$$ENTATION With the CharacterController component, you do not need to add Rigibody or Collider components. This is part of my controller script, which will move your character facing the direction, if the character must rotate a bit larger than rotateLimit, it will stop the character again. You need to call $$anonymous$$ove () each frame so the character can fall down.
private bool is$$anonymous$$oving = false;
public bool Is$$anonymous$$oving
{
get { return is$$anonymous$$oving; }
private set
{
if (is$$anonymous$$oving != value)
{
if (!is$$anonymous$$oving)
{
bootSpeed = 0;
}
is$$anonymous$$oving = value;
}
}
}
public void PlayIdelAnimation()
{
Is$$anonymous$$oving = false;
Anim.SetBool(RunningHash, false);
}
public void PlayRunningAnimation()
{
Is$$anonymous$$oving = true;
Anim.SetBool(RunningHash, true);
}
public void $$anonymous$$ove(Vector3 direction)
{
float angle = Vector3.Angle(transform.forward, direction);
bool hasLock$$anonymous$$ove = angle >= RotateLimit;
bool has$$anonymous$$ove = (direction.x != 0 || direction.z != 0) && (!hasLock$$anonymous$$ove);
if (angle > 0)
{
Quaternion target = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * rotateSpeed);
}
if (has$$anonymous$$ove)
{
PlayRunningAnimation();
bootSpeed += Time.deltaTime * bootSpeed$$anonymous$$ultiple;
bootSpeed = $$anonymous$$athf.Clamp01(bootSpeed);
direction.Normalize();
CharacterController.Simple$$anonymous$$ove(direction * movementSpeed * bootSpeed);
}
else
{
PlayIdelAnimation();
CharacterController.Simple$$anonymous$$ove(Vector3.zero);
}
}
Answer by $$anonymous$$ · Oct 05, 2017 at 04:48 AM
Your script is fine, and as long as there is a collider, there's nothing wrong there.
I think it's a problem with your character's rigidbody. Make sure your character has mass, uses gravity and is not kinematic. Also, freeze your character's x, y and z rotations so it doesn't fall over.
I used virtually the same script as you and had the same problem. Hope this helps!
Answer by FortisVenaliter · Aug 31, 2017 at 09:04 PM
Yes, your code doesn't ever check for those colliders. You need to handle collisions for them to make any difference.
Unity only applies collisions automatically to Rigidbodies, which are driven by physics. Since you are driving the motion of this object through your code instead, you need to write the collision handling as well.
Answer by coolstud243 · Sep 01, 2017 at 05:49 AM
Okay thanks could you possibly point me torwards a link to help me understand this concept a bit more? Such as a tutorial/Example of where the collision is called upon in the code.
Answer by Chief_Ed · Sep 01, 2017 at 03:59 PM
Hi, I am not an expert, but: An easier potential fix for this symptom is to look at the "Rigidbody" component of your player/character and make sure the "Is Kimematic" property is UNCHECKED.
This might do it.
-Ed
Your answer
![](https://koobas.hobune.stream/wayback/20220612133940im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with softbodies collisions 0 Answers
move character at constant speed, and detect collisions. 1 Answer
How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers
How to Have Two Child Objects of One Parent Detect Collisions Between Eachother. 1 Answer