Question by
AustinNSx · Apr 10, 2018 at 06:50 PM ·
c#scripting problemontriggerenter
Having troubles changing my players speed on collision
I cant seem to make my character increase his speed on collision. I am trying to create another object as a powerup so he can walk faster. I could detect the collision just fine, but the speed (velocity) wont change. This is my entire controller code. My problem is between lines 70 -75
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//<summary>
// 1. 8-Directional Movement
// 2. Stop and face current direction when input is absent
// </summary>
public class MarioController : MonoBehaviour {
public Rigidbody rb;
public float velocity = 5;
public float turnSpeed = 10;
public float jumpHeight = 5;
Vector2 input;
float angle;
Quaternion targetRotation;
Transform cam;
Animator anim;
private int count;
public Text countText;
public Text winText;
// Use this for initialization
void Start () {
rb = GetComponent <Rigidbody>();
cam = Camera.main.transform;
anim = GetComponent<Animator>();
count = 0;
SetCountText ();
winText.text = "";
}
// Update is called once per frame
void Update () {
if (Input.GetKey("left shift"))
velocity = 15;
if (!Input.GetKey ("left shift"))
velocity = 10;
if (Input.GetKeyDown (KeyCode.Space)){
rb.AddForce(new Vector3(0, 20, 0), ForceMode.Impulse);
}
GetInput();
if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1) return;
CalculateDirection();
Rotate();
Move();
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag ("Basic_Score")) {
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
if (other.gameObject.CompareTag ("Speed_Capsule")) {
velocity = 25;
}
}
void SetCountText ()
{
countText.text = "Score: " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win!";
}
}
//summary
// input based on Horizontal(a,d,<,>) and Vertical (w,s,^,v) keys
void GetInput() {
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
anim.SetFloat("BlendX", input.x);
anim.SetFloat("BlendY", input.y);
}
//summary
//Direction relative to the camera's rotation
void CalculateDirection() {
angle = Mathf.Atan2(input.x, input.y);
angle = Mathf.Rad2Deg * angle;
angle += cam.eulerAngles.y;
}
//summary
//rotate toward the calculated angle
void Rotate() {
targetRotation = Quaternion.Euler(0, angle, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
}
//summary
//This player only moves along its own forward axis
void Move() {
transform.position += transform.forward * velocity * Time.deltaTime;
}
}
Comment