- Home /
movement control scripts are interfering with eachother in my 2D Unity Game
Hi ppl I am currently coding on an isometric 2D game. Right now im finished with basic movement controls and set up the animations, now i wanted to implement a dash to cursor. I handle this in a seperate script. When I use both scripts on my character he can walk just fine and the cursor works aswell but his dash doesnt work until i turn of the first skript which handles wasd/arrowkeys to move. I want to be abled to use both walking and dash on the character without turning scripts on and off. here are the scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class controls : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
and here is the dasj script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouseDash : MonoBehaviour
{
public float dashSpeed = 5f;
public Rigidbody2D rb;
public Vector3 mousePosition;
public Vector3 direction;
private float timer;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Click");
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
direction = (mousePosition - transform.position).normalized;
rb.velocity = new Vector2(direction.x * dashSpeed, direction.y * dashSpeed);
timer = 1;
}
timer = timer - Time.deltaTime;
if (timer <= 0)
{
rb.velocity = new Vector2(0, 0);
}
}
}
I need a way to use both movement options on the character.
thanks in advance.
Answer by logicandchaos · Aug 26, 2020 at 12:21 AM
you should make the input a separate script and each action as separate scripts.
Can u explain that a bit more in detail? Im new to Coding for games, only have expierience in Python for my Uni.
Well in your wasd movement, you could have a bool called IsDashing and then only run your movement when IsDashing is equal to false, then when youve finished dashing, you can still move using wasd. The problem I believe is that if youre able to move whilst dashing, the dashing breaks or seems unrealistic, so you shouldnt be allowed to do both anyways.