On collision with enemy slow player down while inside the collision box, else normal speed.
Hiya guys this is the code for movement for my Main Player.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public float moveSpeed = 1f;
void Update()
{
Vector3 moveInput = new Vector3(Input.GetAxis("Horizontal"), 0f, -Input.GetAxis("Vertical"));
Vector3 moveVelocity = moveInput * moveSpeed;
gameObject.transform.position += moveVelocity;
float angle = Mathf.Atan2(Input.GetAxisRaw("Horizontal2"), -Input.GetAxisRaw("Vertical2")) * Mathf.Rad2Deg;
if (Input.GetAxis("Horizontal2")!= 0||-Input.GetAxis("Vertical2")!=0);
{
gameObject.transform.rotation = Quaternion.AngleAxis(angle, gameObject.transform.up);
}
}
}
What I am trying to do is, when the Main character collides with my an Enemy, while in its collision boss I want the moveSpeed to be halved until its out of the collision box where normal movespeed will be returned. I have tried this
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public float moveSpeed = 1f;
// public GameObject Bullet_Emitter;
// public GameObject Projectile;
// public float Bullet_Forward_Force;
void Update()
{
Vector3 moveInput = new Vector3(Input.GetAxis("Horizontal"), 0f, -Input.GetAxis("Vertical"));
Vector3 moveVelocity = moveInput * moveSpeed;
gameObject.transform.position += moveVelocity;
float angle = Mathf.Atan2(Input.GetAxisRaw("Horizontal2"), -Input.GetAxisRaw("Vertical2")) * Mathf.Rad2Deg;
if (Input.GetAxis("Horizontal2")!= 0||-Input.GetAxis("Vertical2")!=0);
{
gameObject.transform.rotation = Quaternion.AngleAxis(angle, gameObject.transform.up);
}
}
void OnCollisionEnter(Collision collision)
{
if (gameObject.tag == "Fungus")
{
Debug.Log("HIT");
moveSpeed /= 2f;
}
}
}
Any help would be appreciated! Thank you!
with a collider you won't really get inside the box. I recommend using the box as a trigger (check IsTrigger) and use OnTriggerEnter and OnTriggerExit for where you change moveSpeed
Your answer
Follow this Question
Related Questions
Texture overlay in sphere? 0 Answers
How to open a dwg file which is basically a autocad format file 2 Answers
,failed configuration 0 Answers
Using Two Camera - One for Zooming in and Out And The other For Gameplay 0 Answers
Bounce ball after collision 1 Answer