using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shotgunKnockback : MonoBehaviour
{
// Start is called before the first frame update
public CharacterController controller;
public float speed = 12f;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = (transform.right * x + transform.forward * z) * -500f;
controller.Move(move * speed * Time.deltaTime);
}
}
It looks like you are trying to also handle character run movement too? Here’s an example of how you would do both:
public class shotgunKnockback : MonoBehaviour {
public CharacterController controller;
public float speed = 12f;
//how far back the knockback goes
public float knockbackDistance = 0.5f;
void Update()
{
// get player inputs
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
// multiply player inputs by speed, and deltaTime, to get distance to move this frame.
// store that in "move"
Vector3 move = (transform.right * x + transform.forward * z) * speed * Time.deltaTime;
//if we clicked
if (Input.GetMouseButtonDown(0))
{
// multiply the knockback by the backward direction, and add that to our "move" variable
move += -transform.forward * knockbackDistance
}
// move the character by the stored amount
controller.Move(move);
}
}
If you only wanted the knockback, and no walking movement, all you need is this:
public class shotgunKnockback : MonoBehaviour {
public CharacterController controller;
//how far back the knockback goes
public float knockbackDistance = 0.5f;
void Update()
{
//if we clicked
if (Input.GetMouseButtonDown(0))
{
// multiply the knockback by the backward direction, and move
controller.Move(-transform.forward * knockbackDistance)
}
}
}