Hello everyone!
I am fairly new to coding in C# and I can not figure out how to make my player move up and down a slope smoothly and without bouncing. Yesterday I spent hours following youtube videos and guides solving this problem but none of them worked for my game. I tried looking into the scripts and changing them so it would work with my game but no luck since I do not understand a lot of things in the scripts.
Here is my problem: Screen capture - 669f0505b958e49617509da311cd1bb2 - Gyazo
Screen capture - c5700c248ee7a91f30204644874ad533 - Gyazo
Could someone help me figure it out please? I would really appreciate it and I would be thankfull.
This is my character controller
using UnityEngine;
public class asd : MonoBehaviour
{
bool isGrounded;
public Transform groundCheck;
public LayerMask platformlevel;
public float JumpHeight = 550f;
public float MovementSpeed = 5f;
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.2f, platformlevel);
transform.Translate(MovementSpeed * Time.deltaTime, 0f, 0f);
if (Input.GetButtonDown("Jump"))
{
if (isGrounded)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, JumpHeight));
}
}
}
}
First, don’t move Rigidbodies with anything except their methods. Line 21 is fine (it uses AddForce) but line 15 is using transform.Translate and hence is completely bypassing the physics engine, which certainly could be a cause of some jitter.
Use the .MovePosition() and .MoveRotation() methods on the rigidbody instead.
After you do this, if it still steps and jitters at higher lateral speeds, one approach is to raycast down and get the normal where are standing, and use it to tilt your motion so be parallel to the ground.
I tried rewriting transform.Translate but I keep getting this error
“NullReferenceException: Object reference not set to an instance of an object
asd.Update () (at Assets/Scripts/asd.cs:26)”
here is my code
using UnityEngine;
public class asd : MonoBehaviour
{
bool isGrounded;
public Transform groundCheck;
public LayerMask platformlevel;
public float JumpHeight = 550f;
public float MovementSpeed = 5f;
private Rigidbody2D rb2D;
private Vector2 velocity;
void start()
{
velocity = new Vector2(1.75f, 1.1f);
}
void Awake()
{
rb2D = gameObject.AddComponent<Rigidbody2D>();
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.2f, platformlevel);
rb2D.MovePosition(rb2D.position + velocity * Time.deltaTime);
if (Input.GetButtonDown("Jump"))
{
if (isGrounded)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, JumpHeight));
}
}
}
}
any idea whats wrong with it ?
Nope, but you can find out in seconds all by yourself.
The answer is always the same… ALWAYS. It is the single most common error ever.
Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
- also known as: Unassigned Reference Exception
- also known as: Missing Reference Exception
- also known as: Object reference not set to an instance of an object
http://plbm.com/?p=221
The basic steps outlined above are:
- Identify what is null
- Identify why it is null
- Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
https://discussions.unity.com/t/814091/4
Step by step, break it down, find the problem.
Here is a clean analogy of the actual underlying problem of a null reference exception:
https://discussions.unity.com/t/840647/7