Unity Shooting script delay not working - unity3d

I am making a shooter game in Unity. I wanted to delay the shooting script but now my code doesn't seem to be working. Any clues to how I might solve this? Other than the delay function everything worked fine.
Here is the code:
#pragma strict
private var player : GameObject;
public var speed : float;
private var bulletCounter : int;
var reloadtime : float = 2;
private var reloadTimer: float = 0.0;
function Start () {
player = this.gameObject;
}
function Update () {
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
if(Input.GetKey("space")) {
Shoot();
}
}
if(Input.GetKey("w")){
if(player.transform.position.y < 20) {
player.transform.position.y += speed * Time.deltaTime;
}
}
if(Input.GetKey("s")){
if(player.transform.position.y > -20) {
player.transform.position.y -= speed * Time.deltaTime;
}
}
if(Input.GetKey("a")){
if(player.transform.position.x > -20) {
player.transform.position.x -= speed * Time.deltaTime;
}
}
if(Input.GetKey("d")){
if(player.transform.position.x < 20) {
player.transform.position.x += speed * Time.deltaTime;
}
}
}
function Shoot () {
bulletCounter++;
var bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.transform.position = player.transform.position;
bullet.AddComponent.<BulletScript>();
bullet.name = "Bullet"+bulletCounter.ToString();
var audio : AudioSource = GetComponent.<AudioSource>();
audio.Play();
reloadTimer = reloadtime;
}
}

You need to get movement code(W,S,A,D) outside the timer condition -
#pragma strict
private var player : GameObject;
public var speed : float;
private var bulletCounter : int;
var reloadtime : float = 2;
private var reloadTimer: float = 0.0;
function Start () {
player = this.gameObject;
}
function Update () {
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
if(Input.GetKey("space")) {
Shoot();
}
}
}
/********* Move this outside the timer condition *********/
if(Input.GetKey("w")){
if(player.transform.position.y < 20) {
player.transform.position.y += speed * Time.deltaTime;
}
}
if(Input.GetKey("s")){
if(player.transform.position.y > -20) {
player.transform.position.y -= speed * Time.deltaTime;
}
}
if(Input.GetKey("a")){
if(player.transform.position.x > -20) {
player.transform.position.x -= speed * Time.deltaTime;
}
}
if(Input.GetKey("d")){
if(player.transform.position.x < 20) {
player.transform.position.x += speed * Time.deltaTime;
}
}
function Shoot () {
bulletCounter++;
var bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.transform.position = player.transform.position;
bullet.AddComponent.<BulletScript>();
bullet.name = "Bullet"+bulletCounter.ToString();
var audio : AudioSource = GetComponent.<AudioSource>();
audio.Play();
reloadTimer = reloadtime;
}
}
This will keep the movement enabled during wait period(reloadtime) after player shoots.

I think your idea is not so clear. Easiest way to do it would be.
function Shoot () {
...
reloadTimer = Time.time + reloadtime;
}
And if should look like this
if (reloadTimer >= Time.time){
if(Input.GetKey("space")) {
Shoot();
}
}

Related

Player jumps a second time by itself after hitting the ground

I'm trying to make a First Person Controller and after I jump, right as the player is about to hit the ground, it jumps a second time by itself. This doesn't seem to be the case when the player lands on ground higher or lower than the ground it originally jumped on.
This is my Player code:
`
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
[SerializeField] float mouseSensitivity = 3f;
[SerializeField] float walkingSpeed = 10f;
[SerializeField] float flyingSpeed = 15;
[SerializeField] float climbingSpeed = 5f;
[SerializeField] float mass = 1f;
[SerializeField] float acceleration = 20f;
[SerializeField] float worldBottomBoundary = -100f;
public Transform cameraTransform;
public bool IsGrounded => controller.isGrounded;
public float Height
{
get => controller.height;
set => controller.height = value;
}
public event Action OnBeforeMove;
public event Action<bool> OnGroundStateChange;
internal float movementSpeedMultiplier;
State _state;
public State CurrentState
{
get => _state;
set
{
_state = value;
velocity = Vector3.zero;
}
}
public enum State
{
Walking,
Flying,
Climbing
}
CharacterController controller;
internal Vector3 velocity;
Vector2 look;
(Vector3, Quaternion) initialPositionAndRotation;
bool wasGrounded;
PlayerInput playerInput;
InputAction moveAction;
InputAction lookAction;
InputAction flyUpDownAction;
void Awake()
{
controller = GetComponent<CharacterController>();
playerInput = GetComponent<PlayerInput>();
moveAction = playerInput.actions["move"];
lookAction = playerInput.actions["look"];
flyUpDownAction = playerInput.actions["flyUpDown"];
}
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
initialPositionAndRotation = (transform.position, transform.rotation);
}
public void Teleport(Vector3 position, Quaternion rotation)
{
transform.position = position;
Physics.SyncTransforms();
look.x = rotation.eulerAngles.y;
look.y = rotation.eulerAngles.z;
velocity = Vector3.zero;
}
void Update()
{
movementSpeedMultiplier = 1f;
switch (CurrentState)
{
case State.Walking:
UpdateGround();
UpdateGravity();
UpdateMovement();
UpdateLook();
CheckBounds();
break;
case State.Flying:
UpdateMovementFlying();
UpdateLook();
break;
case State.Climbing:
UpdateMovementClimbing();
UpdateLook();
break;
}
}
void CheckBounds()
{
if (transform.position.y < worldBottomBoundary)
{
var (position, rotation) = initialPositionAndRotation;
Teleport(position, rotation);
}
}
void UpdateGround()
{
if (wasGrounded != IsGrounded)
{
OnGroundStateChange?.Invoke(IsGrounded);
wasGrounded = IsGrounded;
}
}
void UpdateGravity()
{
var gravity = Physics.gravity * mass * Time.deltaTime;
velocity.y = IsGrounded ? -1f : velocity.y + gravity.y;
}
Vector3 GetMovementInput(float speed, bool horizontal = true)
{
var moveInput = moveAction.ReadValue<Vector2>();
var flyUpDownInput = flyUpDownAction.ReadValue<float>();
var input = new Vector3();
var referenceTransform = horizontal ? transform : cameraTransform;
input += referenceTransform.forward * moveInput.y;
input += referenceTransform.right * moveInput.x;
if (!horizontal)
{
input += transform.up * flyUpDownInput;
}
input = Vector3.ClampMagnitude(input, 1f);
input *= speed * movementSpeedMultiplier;
return input;
}
void UpdateMovement()
{
OnBeforeMove?.Invoke();
var input = GetMovementInput(walkingSpeed);
var factor = acceleration * Time.deltaTime;
velocity.x = Mathf.Lerp(velocity.x, input.x, factor);
velocity.z = Mathf.Lerp(velocity.z, input.z, factor);
controller.Move(velocity * Time.deltaTime);
}
void UpdateMovementFlying()
{
var input = GetMovementInput(flyingSpeed, false);
var factor = acceleration * Time.deltaTime;
velocity = Vector3.Lerp(velocity, input, factor);
controller.Move(velocity * Time.deltaTime);
}
void UpdateMovementClimbing()
{
var input = GetMovementInput(climbingSpeed, false);
var forwardInputFactor = Vector3.Dot(transform.forward, input.normalized);
if (forwardInputFactor > 0)
{
input.x = input.x * .5f;
input.z = input.z * .5f;
if (Mathf.Abs(input.y) > .2f)
{
input.y = Mathf.Sign(input.y) * climbingSpeed;
}
}
else
{
input.y = 0;
input.x = input.x * 3f;
input.z = input.z * 3f;
}
var factor = acceleration * Time.deltaTime;
velocity = Vector3.Lerp(velocity, input, factor);
controller.Move(velocity * Time.deltaTime);
}
void UpdateLook()
{
var lookInput = lookAction.ReadValue<Vector2>();
look.x += lookInput.x * mouseSensitivity;
look.y += lookInput.y * mouseSensitivity;
look.y = Mathf.Clamp(look.y, -90, 90f);
cameraTransform.localRotation = Quaternion.Euler(-look.y, 0, 0);
transform.localRotation = Quaternion.Euler(0, look.x, 0);
}
void OnToggleFlying()
{
CurrentState = CurrentState == State.Flying ? State.Walking : State.Flying;
}
}
`
And this is my PlayerJumping code:
`
using UnityEngine;
[RequireComponent(typeof(Player))]
public class PlayerJumping : MonoBehaviour
{
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float jumpPressBufferTime = .05f;
[SerializeField] float jumpGroundGraceTime = .2f;
[SerializeField] int maxJumps = 1;
Player player;
bool tryingToJump;
float lastJumpPressTime;
float lastGroundedTime;
int jumps;
void Awake()
{
player = GetComponent<Player>();
}
void OnEnable()
{
player.OnBeforeMove += OnBeforeMove;
player.OnGroundStateChange += OnGroundStateChange;
}
void OnDisable()
{
player.OnBeforeMove -= OnBeforeMove;
player.OnGroundStateChange -= OnGroundStateChange;
}
void OnJump()
{
tryingToJump = true;
lastJumpPressTime = Time.time;
}
void OnBeforeMove()
{
if (player.IsGrounded) jumps = 0;
var wasTryingToJump = Time.time - lastJumpPressTime < jumpPressBufferTime;
var wasGrounded = Time.time - lastGroundedTime < jumpGroundGraceTime;
var isOrWasTryingToJump = tryingToJump || (wasTryingToJump && player.IsGrounded);
var isOrWasGrounded = player.IsGrounded || wasGrounded;
var jumpAllowed = jumps < maxJumps;
if (
jumpAllowed && isOrWasTryingToJump && isOrWasGrounded
|| jumpAllowed && tryingToJump
)
{
player.velocity.y += jumpSpeed;
jumps++;
}
tryingToJump = false;
}
void OnGroundStateChange(bool isGrounded)
{
if (!isGrounded) lastGroundedTime = Time.time;
}
}
`
Although to be honest, I've checked a million times and I'm starting to believe the problem isn't in the code but in the inspector, but don't let that cloud your judgement because I can be pretty stupid at times. Do you have any ideas?

My playable character turns left or right when it meets an obstacle

My playable character turns left or right when it meets an obstacle with a collider. It's normal but I want to know if there is a way to disable it.
this is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : MonoBehaviour
{
public Vector3 startPosition;
private const float LANE_DISTANCE = 3.0f;
private const float TURN_SPEED = 0.5f;
//Functionality
private bool isRunning = false;
public bool isClimbing = false;
private readonly object down;
private CharacterController controller;
[SerializeField]
private float jumpForce = 5.0f;
private float verticalVelocity = 0.0f;
private float gravity = 10.0f;
//Speed
private float originalSpeed = 4.0f;
private float speed = 4.0f;
private float speedIncreaseLastTick;
private float speedIncreaseTime = 2.5f;
private float speedIncreaseAmount = 0.1f;
private float climbingSpeed = 1.0f;
private int desiredLane = 0; //0 = left, 1 = middle, 2 = right
private Animator anim;
// Start is called before the first frame update
void Start()
{
speed = originalSpeed;
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
transform.position = startPosition;
}
// Update is called once per frame
void Update()
{
if (isClimbing)
{
transform.Translate(Vector3.up * climbingSpeed * Time.deltaTime);
}
if (!isRunning)
return;
if (Time.time - speedIncreaseLastTick > speedIncreaseTime)
{
speedIncreaseLastTick = Time.time;
speed += speedIncreaseAmount;
//GameManager.Instance.UpdateScores();
}
// Gather the inputs on wich lane we should be
if (MobileInput.Instance.SwipeLeft)
{
MoveLane(false);
}
if (MobileInput.Instance.SwipeRight)
{
MoveLane(true);
}
// Calculate where we should be horizontally
Vector3 targetPosition = transform.position.z * Vector3.forward;
int posX = Mathf.Abs(desiredLane);
if (desiredLane < 0)
targetPosition += Vector3.left * posX * LANE_DISTANCE;
else if (desiredLane > 0)
targetPosition += Vector3.right * posX * LANE_DISTANCE;
//Calculate move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
bool isGrounded = IsGrounded();
anim.SetBool("Grounded", isGrounded);
//Calculate y
if (isGrounded) //If grounded
{
verticalVelocity = -0.1f;
if (MobileInput.Instance.SwipeUp)
{
//Jump
anim.SetTrigger("Jump");
verticalVelocity = jumpForce;
}
else if (MobileInput.Instance.SwipeDown)
{
//Slide
StartSliding();
Invoke("StopSliding", 1.0f);
}
}
else
{
verticalVelocity -= (gravity * Time.deltaTime);
//Fast falling machanics
if (MobileInput.Instance.SwipeDown)
{
verticalVelocity = -jumpForce;
}
}
moveVector.y = verticalVelocity;
moveVector.z = speed;
//Move the character
controller.Move(moveVector * Time.deltaTime);
//Rotate the player where is going
Vector3 dir = controller.velocity;
if (dir!= Vector3.zero)
{
dir.y = 0;
transform.forward = Vector3.Lerp(transform.forward, dir, TURN_SPEED);
}
}
// This function (MoveLane) allows the player to move to the left and to the right
private void MoveLane(bool goingRight)
{
if (!goingRight)
{
desiredLane--;
if (desiredLane == -6)
desiredLane = -5;
}
if (goingRight)
{
desiredLane++;
if (desiredLane == 6)
desiredLane = 5;
}
/* We wan rewrite the above function like this below
desiredLane += (goingRight) ? 1 : -1;
Mathf.Clamp(desiredLane, -5, 5);
*/
}
private bool IsGrounded()
{
Ray groundRay = new Ray(new Vector3(controller.bounds.center.x, (controller.bounds.center.y - controller.bounds.extents.y) + 0.2f,
controller.bounds.center.z), Vector3.down);
Debug.DrawRay(groundRay.origin, groundRay.direction, Color.cyan, 1.0f);
return (Physics.Raycast(groundRay, 0.2f + 0.1f));
}
public void StartRunning ()
{
isRunning = true;
anim.SetTrigger("StartRunning");
}
private void StartSliding()
{
anim.SetBool("Sliding", true);
controller.height /= 2;
controller.center = new Vector3(controller.center.x, controller.center.y / 2, controller.center.z);
}
private void StopSliding()
{
anim.SetBool("Sliding", false);
controller.height *= 2;
controller.center = new Vector3(controller.center.x, controller.center.y * 2, controller.center.z);
}
private void Crash()
{
anim.SetTrigger("Death");
isRunning = false;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
switch(hit.gameObject.tag)
{
case "Obstacle":
Crash();
break;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Ladder")
{
isRunning = false;
isClimbing = true;
anim.SetBool("ClimbingLadder", true);
}
else if (other.gameObject.tag == "LadderCol2")
{
isClimbing = false;
anim.SetBool("ClimbingLadder", false);
transform.Translate(Vector3.forward * 1);
isRunning = true;
}
}
}
I see the problem. It's from these lines
Vector3 dir = controller.velocity;
if (dir!= Vector3.zero)
{ dir.y = 0;
transform.forward =
Vector3.Lerp(transform.forward, dir,
TURN_SPEED);
}
I added them to rotate a bit the player when it turns left or right.

How can I make my character move only forward in Unity?

I have a character in Unity which use a move script, to transform its position.x, y, or z everytime when I press the appropriate button. Now I would like to change my code to just rotate the character when the user press the "a" or "d" and don't move it, however if the user press the "w" button move it forward.
I have already looked for it on the internet, and found out that I would need to use Vector3.forward somehow, but it doesn't work.
Here is my script:
This is the script which actually moves the character:
(It is attached to a Player object which contains the moveable characters)
public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;
Vector3 startPos;
Vector3 endPos;
bool firstInput;
public bool justJump;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
if (perc == 1) {
lerpTime = 1;
currentLerpTime = 0;
firstInput = true;
justJump = true;
}
}
startPos = gameObject.transform.position;
if (Input.GetButtonDown("right") && gameObject.transform.position == endPos) {
endPos = new Vector3(transform.position.x + 0.5f, transform.position.y, transform.position.z);
}
if (Input.GetButtonDown("left") && gameObject.transform.position == endPos) {
endPos = new Vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z);
}
if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 0.5f);
}
if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 0.5f);
}
if (firstInput == true) {
currentLerpTime += Time.deltaTime * 5;
perc = currentLerpTime / lerpTime;
gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
if (perc > 0.8f) {
perc = 1;
}
if (Mathf.Round(perc) == 1) {
justJump = false;
}
}
}
}
And here is my "rotate script", which is attached to the moveable character(s) itself:
public class AnimationController : MonoBehaviour {
Animator anim;
public GameObject thePlayer;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Move moveScript = thePlayer.GetComponent<Move>();
if (moveScript.justJump == true) {
anim.SetBool("Jump", true);
}
else {
anim.SetBool("Jump", false);
}
if (Input.GetButtonDown("right")) {
gameObject.transform.rotation = Quaternion.Euler(0, 90, 0);
}
if (Input.GetButtonDown("left")) {
gameObject.transform.rotation = Quaternion.Euler(0, -90, 0);
}
if (Input.GetButtonDown("up")) {
gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
}
if (Input.GetButtonDown("down")) {
gameObject.transform.rotation = Quaternion.Euler(0, 180, 0);
}
}
}
Could you give me please any help on this? (Preferably with code, if you can :) )
Assuming you want your character to move to where it is facing, and trying not to do a lot of changes to your code, this is what could be done:
First, attach both your script directly to your movable character.
Then, change your Move script to:
public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;
Vector3 startPos;
Vector3 endPos;
bool firstInput;
public bool justJump;
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
if (perc == 1) {
lerpTime = 1;
currentLerpTime = 0;
firstInput = true;
justJump = true;
}
}
startPos = gameObject.transform.position;
if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
}
if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
}
if (firstInput == true) {
currentLerpTime += Time.deltaTime * 5;
perc = currentLerpTime / lerpTime;
gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
if (perc > 0.8f) {
perc = 1;
}
if (Mathf.Round(perc) == 1) {
justJump = false;
}
}
}
}
And your AnimationController script to
public class AnimationController : MonoBehaviour {
Animator anim;
public GameObject thePlayer;
// Use this for initialization
void Start () {
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
Move moveScript = thePlayer.GetComponent<Move>();
if (moveScript.justJump == true) {
anim.SetBool("Jump", true);
}
else {
anim.SetBool("Jump", false);
}
if (Input.GetButtonDown("right")) {
gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
}
if (Input.GetButtonDown("left")) {
gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
}
}
}
This will make your character rotate only when you press the left and right buttons, and move only when you press the up and down button.
Althought this solves your problem, this is not the best way to write scripts for your character movement.
I recommend you read more about the state machine design patter. This book by Robert Nystrom has a chapter about it and is really easy to read. Also, its free to read online :)

Unity Fade delay when game ends

I'm fairly new at Unity and i'm trying to making a game.
I want to have an subtitle fading in when you're at the end of the game. This start when you hit a button.
But when I code a image that I fadein, it plays it directly when you start the game.
Do you guys know a solution?
#pragma strict
private var guiShow : boolean = false;
var car : GameObject;
var rayLength = 10;
var guiObject : GUITexture;
var fadeTime = 1.0;
enum Fade {In, Out}
var fadesubtitles : boolean = false;
function Update ()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "car")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
guiShow = false;
}
else if(Input.GetKeyDown("e"))
{
guiShow = false;
}
}
}
else
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 150, 25), "Press F to escape");
if(Input.GetKeyDown("f")){
fadesubtitles = true;
}
}
}
if (fadesubtitles == true){
yield FadeGUITexture(guiObject, fadeTime, Fade.In);
yield WaitForSeconds(3.0);
yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
}
function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
if (subtitles == true){
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}
}
I'd start your game object in the 'disabled' state (uncheck it in the inspector). Then at then end of the game, have some code that enables it.
You can use iTween.
FadeFrom(GameObject target, Hashtable args)
Example:
iTween.FadeFrom(gameObject, iTween.Hash("alpha", 0f, "amount", 1f, "time", 2f));

How Can I Reset my Car Movement if I return to my last respawn point?

function OnTriggerEnter(col : Collider){
if(col.tag == "Player")
{
player.transform.position = SpawnPoint.position;
audio.PlayOneShot(Sound);
VioSign.enabled = true;
if(pauseEnabled == false){
pauseEnabled = true;
AudioListener.volume = 1;
Time.timeScale = 0;
Screen.showCursor = true;
}
}
}
This is my respawnpoint script .
#pragma strict
var wheelFL : WheelCollider;
var wheelFR : WheelCollider;
var wheelRL : WheelCollider;
var wheelRR : WheelCollider;
var maxTorque : float = 50;
function Start(){
rigidbody.centerOfMass.y = -0.9;
}
function FixedUpdate () {
wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelFL.steerAngle = 20 * Input.GetAxis("Horizontal");
wheelFR.steerAngle = 20 * Input.GetAxis("Horizontal");
}
Here is my Car Control Script I want to stop the car after respawning again to the respawn point . I'm having a difficult time to solve it. please help me :)
If you just want to stop the car's current movement, you can simply set its velocity to zero:
function OnTriggerEnter(col : Collider){
if(col.tag == "Player")
{
player.transform.position = SpawnPoint.position;
player.gameObject.rigidBody.velocity = Vector3.zero;
audio.PlayOneShot(Sound);
VioSign.enabled = true;
if(pauseEnabled == false){
pauseEnabled = true;
AudioListener.volume = 1;
Time.timeScale = 0;
Screen.showCursor = true;
}
}
}