This script doesnt work why? (2D)
Hi i made an character controller that lets me move in air slower than on the ground and i have no idea why it doesnt work i changed the script 3 times and for me it should work. If someone has the time to read through it THANKS! The movement only works if i change the Realacceleration to acceleration... but then i have the same acceleration in the air as on the ground. When i start this script now my character just doesnt move...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playermovement : MonoBehaviour
{
public float acceleration = 0f;
public float maxSpeed = 0f;
public float airmovement = 0f;
public float distToGround = 0f;
public Animator anim;
private Vector2 RealAcceleration = new Vector2 (0,0);
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
}
void FixedUpdate()
{
//Gets input
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVertical = Input.GetAxisRaw("Vertical");
Vector2 moveInput = new Vector2(moveHorizontal, 0);
// The code that should change the input when the Character is in the air
if (isGrounded())
{
Vector2 Realacceleration = new Vector2 (moveInput.x * acceleration,0);
}
if (isGrounded() == false)
{
Vector2 Realacceleration = new Vector2(moveInput.x * acceleration / airmovement, 0);
}
Debug.Log(acceleration);
Debug.Log(airmovement);
Debug.Log(RealAcceleration);
rb.AddForce(RealAcceleration);
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
bool isGrounded()
{
Vector3 Raymove = new Vector3(0, 0.8f, 0);
return Physics2D.Raycast(transform.position - Raymove, Vector3.down, distToGround);
}
}
}
Well, you are multiplying by acceleration which is zero so you are applying no acceleration since multiplying by zero makes zero. That's why it's not moving in this case. I don't really know exactly the effect you are going for to begin with but if it's to give your character that old $$anonymous$$ario feel where you have air control left and right, then you'll just make the acceleration smaller. Ins$$anonymous$$d of dividing by airmovment try making groundAcceleration and airAcceleration variables to multiply to your horz. movement controls. That will give you more control over it. Also, you'll want your control input and logic in Update(). Your physics is in the right place in FixedUpdate(), though.
The thing is the acceleration = 0f was just a placeholder and in the Engine i changed the parameters...
Also, don't new Vector2() your RealAcceleration in the control logic, just Vector2(). And consider rena$$anonymous$$g to realAcceleration to keep your na$$anonymous$$g consistant and code more readable. I was actually looking for where you defined the method or Type at first glance...
Thanks, that was btw my first script and i did learn a lot since then! If i look at it soo much things dont make any sense.
Your answer
Follow this Question
Related Questions
Check if Dump's velocity is zero 0 Answers
Unity2D Help with rigidbody character movement. 0 Answers
2D Animation Delay 3 Answers
Player Movement Not Always Responding 1 Answer
Isometric movement for MoveTowards 0 Answers